Replace configuration in app.config with code configuration for WCF service

I am trying to connect to DevelpmentServicefrom MS CRM from a custom plugin, and therefore I can’t use the app.configone generated when adding WebReference to the solution.

Here is the working code:

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

And the corresponding part 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>

Is it possible to convert the code if the configuration file is not needed. How?

+4
source share
3 answers

He should work

 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 you can find an example of using code instead of a configuration file.

+1
source

, - VB #. , , , .

, - :

//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

it works on my living environment

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

where Identity = new SpnEndpointIdentity("")andCredentials = new NetworkCredential("User", "Pass", "server")

Using

NameSpace.UseService("http://server/XRMDeployment/2011/Deployment.svc",
                     Identity,
                     Credentials,
(IOrganizationService context) =>
{ 
   ... your code here ...
   return true;
});

this suggests that Windows Authentication and SpnEndpointIdentity have a very long base64 string. I am not sure if you have this case.

in catch, you can do some error handling or re-examination, but in my case it is not used.

+1
source

All Articles