如何以编程方式更改端点的身份配置? [英] How to programmatically change an endpoint's identity configuration?

查看:136
本文介绍了如何以编程方式更改端点的身份配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个工具,以编程方式修改我的服务的app.config文件.代码是这样的,

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.

我想要类似的东西

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

它失败,因为属性 endpoint.Identity identityElement.UserPrincipalName 是只读的(我不确定为什么,因为entity.Address不是只读的)

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.

我确实看到了另一个问题的答案, https://stackoverflow.com/a/2068075/81251 ,它使用标准XML操作来更改端点地址.这是一个hack,但它可能会做您想要的.

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:

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

这篇关于如何以编程方式更改端点的身份配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆