I am trying to create a tool to modify my service’s app.config file programmatically. The code is something like this,
string _configurationPath = @"D:\MyService.exe.config";
ExeConfigurationFileMap executionFileMap = new ExeConfigurationFileMap();
executionFileMap.ExeConfigFilename = _configurationPath;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(executionFileMap, ConfigurationUserLevel.None);
ServiceModelSectionGroup serviceModeGroup = ServiceModelSectionGroup.GetSectionGroup(config);
foreach (ChannelEndpointElement endpoint in serviceModeGroup.Client.Endpoints)
{
if (endpoint.Name == "WSHttpBinding_IMyService")
{
endpoint.Address = new Uri("http://localhost:8080/");
}
}
config.SaveAs(@"D:\MyService.exe.config");
However I have problem changing the endpoint’s identity.
I want to have something like:
<identity>
<userPrincipalName value="user@domain.com" />
</identity>
for my endpoint configuration, but when i try :
endpoint.Identity = new IdentityElement(){
UserPrincipalName = UserPrincipalNameElement() { Value = "user@domain.com" }
}
It fails because the property endpoint.Identity and identityElement.UserPrincipalName is readonly (I’m not sure why, because entity.Address is not read-only)
Is there any way to get around this restriction and set the identity configuration?
I don’t think there is a way to do this built into the framework, at least I haven’t seen anything easy but could be wrong.
I did see an answer to a different question, https://stackoverflow.com/a/2068075/81251, that uses standard XML manipulation to change the endpoint address. It is a hack, but it would probably do what you want.
Below is the code from the linked answer, for the sake of completeness: