我正在尝试创建一个工具来以编程方式修改我的服务的app.config文件。代码是这样的,
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");但是,我在更改端点的身份时遇到了问题。
我想要这样的东西:
<identity>
<userPrincipalName value="user@domain.com" />
</identity>用于我的端点配置,但当我尝试时:
endpoint.Identity = new IdentityElement(){
UserPrincipalName = UserPrincipalNameElement() { Value = "user@domain.com" }
}它失败是因为属性endpoint.Identity和identityElement.UserPrincipalName是只读的(我不确定为什么,因为entity.Address不是只读的)
有没有办法绕过这一限制并设置身份配置?
发布于 2013-05-16 22:35:34
这被确认为正常工作。多痛苦啊。
public static void ChangeClientEnpoint(string name, Uri newAddress)
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup serviceModeGroup = ServiceModelSectionGroup.GetSectionGroup(config);
ChannelEndpointElement existingElement = null;
foreach (ChannelEndpointElement endpoint in serviceModeGroup.Client.Endpoints)
{
if (endpoint.Name == "BasicHttpBinding_IMembershipService")
{
existingElement = endpoint;
}
}
EndpointAddress endpointAddress = new EndpointAddress(newAddress.ToString());
var newElement = new ChannelEndpointElement(endpointAddress, existingElement.Contract)
{
BehaviorConfiguration = existingElement.BehaviorConfiguration,
Binding = existingElement.Binding,
BindingConfiguration = existingElement.BindingConfiguration,
Name = existingElement.Name
// Set other values
};
serviceModeGroup.Client.Endpoints.Remove(existingElement);
serviceModeGroup.Client.Endpoints.Add(newElement);
config.Save();
ConfigurationManager.RefreshSection("system.serviceModel/client");
}https://stackoverflow.com/questions/7387915
复制相似问题