首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何以编程方式更改端点的身份配置?

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

Stack Overflow用户
提问于 2011-09-12 20:33:17
回答 4查看 7.7K关注 0票数 2

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

代码语言:javascript
复制
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");

但是,我在更改端点的身份时遇到了问题。

我想要这样的东西:

代码语言:javascript
复制
<identity>
     <userPrincipalName value="user@domain.com" />
</identity>

用于我的端点配置,但当我尝试时:

代码语言:javascript
复制
endpoint.Identity = new IdentityElement(){
    UserPrincipalName = UserPrincipalNameElement() { Value = "user@domain.com" }
}

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

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

EN

Stack Overflow用户

发布于 2013-05-16 22:35:34

这被确认为正常工作。多痛苦啊。

代码语言:javascript
复制
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");
    }
票数 7
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7387915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档