我正在尝试从自定义插件连接到DevelpmentService
MS CRM,因此当我将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>
当不需要配置文件时,是否可以转换代码。怎么样?
答案 0 :(得分:1)
是的,您可以使用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
{
}
答案 1 :(得分:1)
这适用于我的实时环境
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字符串我不确定你是否有这种情况。
在捕获中你可以做一些错误处理或重审,但在我的情况下它没有被使用。
答案 2 :(得分:1)
应该有效
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您可以找到使用代码而不是配置文件
的示例