如何以编程方式更改端点的身份配置?

时间:2011-09-12 12:33:17

标签: .net wcf configuration app-config

我正在尝试创建一个工具来以编程方式修改我的服务的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.IdentityidentityElement.UserPrincipalName是只读的(我不知道为什么,因为entity.Address不是只读的)

有没有办法绕过这个限制并设置身份配置?

4 个答案:

答案 0 :(得分:7)

这被证实有效。多么痛苦......

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");
    }

答案 1 :(得分:2)

对于寻找解决方案的人,我相信你可以通过以下方式实现它(这是前一个例子的一个小扩展):

    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);
    }

答案 2 :(得分:1)

我认为没有办法在框架中构建这个,至少我没有看到任何简单但可能是错误的。

我确实看到了另一个问题的答案,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";
        }
    }
}

答案 3 :(得分:0)

尝试这样的事情......

http://msdn.microsoft.com/en-us/library/bb628618.aspx

    ServiceEndpoint ep = myServiceHost.AddServiceEndpoint(
                typeof(ICalculator),
                new WSHttpBinding(),
                String.Empty);
EndpointAddress myEndpointAdd = new EndpointAddress(new Uri("http://localhost:8088/calc"),
     EndpointIdentity.CreateDnsIdentity("contoso.com"));
ep.Address = myEndpointAdd;