2014-02-25 91 views
4

我試圖從自定義插件連接到MS CRM的DevelpmentService,因此我無法使用在將WebReference添加到解決方案時生成的app.config使用代碼配置替換app.config中的配置WCF服務

這裏是工作代碼:

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> 

是否有可能時,將不再需要配置文件中的方式改變代碼。怎麼樣?

回答

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您可能會發現使用代碼而不是配置文件的例子

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 
{ 
} 
+0

不幸的是沒有工作... – shytikov

+0

我已經給你什麼也只是一個指導.. 。你將不得不依靠自己的價值觀。我不認爲你可以按照我描述的方式使用「http://server/XRMDeployment/2011/2011/Deployment.svc」作爲你的端點地址。你需要一個真實的URL。並且您在「CustomBinding_IDeploymentService」 中定義的值需要添加到我提供的代碼示例的相應位置中。 – Brian

+0

這就是我想要做的,但不幸的是,在我的配置中沒有'contract'的地方。 'EndpointIdentity'根本不用在你的代碼中。 'default(System.ServiceModel.EndpointIdentity)'返回null。 '默認(System.ServiceModel.BasicHttpBinding)'返回null以及... – shytikov

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("") and 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字符串我不知道你有沒有這種情況。

在捕獲你可以做一些錯誤處理或重審,但在我的情況下,它不使用。