[英]Replace configuration in app.config with code configuration for WCF service
我试图连接到DevelpmentService
从自定义插件MS CRM的,因此我不能够使用app.config
当我添加Web引用到溶液中生成。
这是工作代码:
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>
是否可以在不需要配置文件时转换代码。 怎么样?
是的,您可以使用VB或C#在代码中执行所有Web服务或客户端配置。 在某些方面,实际上,最好在代码中进行配置,因为您的代码可以根据变量或现有条件动态配置。
基本上,你可以做这样的事情:
//end point setup
System.ServiceModel.EndpointAddress EndPoint = new System.ServiceModel.EndpointAddress("http://Domain:port/Class/Method");
System.ServiceModel.EndpointIdentity EndpointIdentity = default(System.ServiceModel.EndpointIdentity);
//binding setup
System.ServiceModel.BasicHttpBinding binding = default(System.ServiceModel.BasicHttpBinding);
binding.TransferMode = TransferMode.Streamed;
//add settings
binding.MaxReceivedMessageSize = int.MaxValue;
binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
binding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
binding.ReaderQuotas.MaxDepth = int.MaxValue;
binding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
binding.MaxReceivedMessageSize = int.MaxValue;
binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = System.Text.Encoding.UTF8;
binding.MaxBufferSize = int.MaxValue;
binding.MaxBufferPoolSize = int.MaxValue;
binding.MaxReceivedMessageSize = int.MaxValue;
binding.SendTimeout = new TimeSpan(0, 10, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
//setup for custom binding
System.ServiceModel.Channels.CustomBinding CustomBinding = new System.ServiceModel.Channels.CustomBinding(binding);
我做什么来配置我的合同:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"), System.ServiceModel.ServiceContractAttribute(Namespace = "http://MyNameSpace", ConfigurationName = "IHostInterface")]
public interface IHostInterface
{
}
这是在我的生活环境
public static TResult UseService<TChannel, TResult>(string url,
EndpointIdentity identity,
NetworkCredential credential,
Func<TChannel, TResult> acc)
{
var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
var endPointAddress = new EndpointAddress(new Uri(url), identity,
new AddressHeaderCollection());
var factory = new ChannelFactory<T>(binding, address);
var loginCredentials = new ClientCredentials();
loginCredentials.Windows.ClientCredential = credentials;
foreach (var cred in factory.Endpoint.EndpointBehaviors.Where(b => b is ClientCredentials).ToArray())
factory.Endpoint.EndpointBehaviors.Remove(cred);
factory.Endpoint.EndpointBehaviors.Add(loginCredentials);
TChannel channel = factory.CreateChannel();
bool error = true;
try
{
TResult result = acc(channel);
((IClientChannel)channel).Close();
error = false;
factory.Close();
return result;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return default(TResult);
}
finally
{
if (error)
((IClientChannel)channel).Abort();
}
}
其中Identity = new SpnEndpointIdentity("")
和Credentials = new NetworkCredential("User", "Pass", "server")
用法
NameSpace.UseService("http://server/XRMDeployment/2011/Deployment.svc",
Identity,
Credentials,
(IOrganizationService context) =>
{
... your code here ...
return true;
});
这假设windows身份验证和SpnEndpointIdentity有一个非常长的base64字符串我不确定你是否有这种情况。
在捕获中你可以做一些错误处理或重审,但在我的情况下它没有使用。
它应该工作
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);
在这里,您可以找到使用代码而不是配置文件的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.