我正在尝试创建一个工具来以编程方式修改我的服务的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不是只读的)
有没有办法绕过这一限制并设置身份配置?
发布于 2011-12-08 13:22:43
我不认为有一种方法可以做到这一点内置在框架中,至少我还没有看到任何简单的东西,但可能是错误的。
我确实看到了另一个问题的答案,即https://stackoverflow.com/a/2068075/81251,它使用标准的XML操作来更改端点地址。这是一个黑客,但它可能会做你想要的。
为了完整起见,以下是链接答案中的代码:
using System;
using System.Xml;
using System.Configuration;
using System.Reflection;
namespace Glenlough.Generations.SupervisorII
{
public class ConfigSettings
{
private static string NodePath = "//system.serviceModel//client//endpoint";
private ConfigSettings() { }
public static string GetEndpointAddress()
{
return ConfigSettings.loadConfigDocument().SelectSingleNode(NodePath).Attributes["address"].Value;
}
public static void SaveEndpointAddress(string endpointAddress)
{
// load config document for current assembly
XmlDocument doc = loadConfigDocument();
// retrieve appSettings node
XmlNode node = doc.SelectSingleNode(NodePath);
if (node == null)
throw new InvalidOperationException("Error. Could not find endpoint node in config file.");
try
{
// select the 'add' element that contains the key
//XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
node.Attributes["address"].Value = endpointAddress;
doc.Save(getConfigFilePath());
}
catch( Exception e )
{
throw e;
}
}
public static XmlDocument loadConfigDocument()
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(getConfigFilePath());
return doc;
}
catch (System.IO.FileNotFoundException e)
{
throw new Exception("No configuration file found.", e);
}
}
private static string getConfigFilePath()
{
return Assembly.GetExecutingAssembly().Location + ".config";
}
}
}发布于 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");
}发布于 2013-03-07 09:46:18
对于任何正在寻找解决方案的人来说,我相信您可以通过以下方式实现(这是前面示例的一个小扩展):
public void ChangeMyEndpointConfig(ChannelEndpointElement existingElement, ServiceModelSectionGroup grp, string identityValue)
{
EndpointAddress endpointAddress = new EndpointAddress(existingElement.Address, EndpointIdentity.CreateDnsIdentity(identityValue));
var newElement = new ChannelEndpointElement(endpointAddress, existingElement.Contract)
{
BehaviorConfiguration = existingElement.BehaviorConfiguration,
Binding = existingElement.Binding,
BindingConfiguration = existingElement.BindingConfiguration,
Name = existingElement.Name
// Set other values
};
grp.Client.Endpoints.Remove(existingElement);
grp.Client.Endpoints.Add(newElement);
}https://stackoverflow.com/questions/7387915
复制相似问题