gpt4 book ai didi

c# - 将 app.config 中的配置替换为 WCF 服务的代码配置

转载 作者:太空狗 更新时间:2023-10-30 01:36:31 24 4
gpt4 key购买 nike

我正在尝试从自定义插件连接到 MS CRM 的 DevelpmentService,因此我无法使用将 WebReference 添加到解决方案时生成的 app.config .

这是工作代码:

var id = new EntityInstanceId
{
Id = new Guid("682f3258-48ff-e211-857a-2c27d745b005")
};

var client = new DeploymentServiceClient("CustomBinding_IDeploymentService");

var organization = (Organization)client.Retrieve(DeploymentEntityType.Organization, id);

app.config的相应部分:

<client>
<endpoint address="http://server/XRMDeployment/2011/Deployment.svc"
binding="customBinding" bindingConfiguration="CustomBinding_IDeploymentService"
contract="DeploymentService.IDeploymentService" name="CustomBinding_IDeploymentService">
<identity>
<userPrincipalName value="DOMAIN\DYNAMICS_CRM" />
</identity>
</endpoint>

...

</client>

是否可以在不需要配置文件的情况下转换代码。怎么办?

最佳答案

它应该工作

 var id = new EntityInstanceId
{
Id = new Guid("682f3258-48ff-e211-857a-2c27d745b005")
};

var endpoint = new EndpointAddress(new Uri("http://server/XRMDeployment/2011/Deployment.svc"),
EndpointIdentity.CreateUpnIdentity(@"DOMAIN\DYNAMICS_CRM"));

var login = new ClientCredentials();
login.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

var binding = new CustomBinding();

//Here you may config your binding (example in the link at the bottom of the post)

var client = new DeploymentServiceClient(binding, endpoint);

foreach (var credential in client.Endpoint.Behaviors.Where(b => b is ClientCredentials).ToArray())
{
client.Endpoint.Behaviors.Remove(credential);
}

client.Endpoint.Behaviors.Add(login);
var organization = (Organization)client.Retrieve(DeploymentEntityType.Organization, id);

Here您可能会发现使用代码而不是配置文件的示例

关于c# - 将 app.config 中的配置替换为 WCF 服务的代码配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22015884/

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