gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:22:09 25 4
gpt4 key购买 nike

我正在尝试创建一个工具来以编程方式修改我的服务的 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 不是只读的)

有什么方法可以绕过这个限制并设置身份配置吗?

最佳答案

这被确认为有效。好痛……

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

关于.net - 如何以编程方式更改端点的身份配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7387915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com