Posted on 27. September 2013

Microsoft.Xrm.Client (Part 3b): Configuration via app/web.config

In this series we have been looking at the Developer Extensions provided by the Microsoft.Xrm.Client assembly:

Part 1 - CrmOrganizationServiceContext and when should I use it?

Part 2 - Simplified Connection Management & Thread Safety

Part 3a – CachedOrganizationService

So far in this series we have learnt how to use the Microsoft.Xrm.Client developer extensions to simplify connecting to Dynamics CRM, handling thread safety and client side caching.

This post shows how the configuration of the Dynamics CRM connection and associated OrganizationService context can be configured using an app.config or web.config file. This is esspecially useful with ASP.NET Portals and Client Applications that don't need the facility to dynamically connect to different Dynamics CRM Servers.

If you only want to configure connection string, then you would add a app.config or web.config with the following entry:

<connectionStrings>
<add name="Xrm" connectionString="Url=http://<server>/<org>"/>
</connectionStrings>

The Connection string can take on the following forms:

On Prem (Windows Auth)

Url=http://<server>/<org>;

On Prem (Windows Auth with specific credentials)

Url=http://<server>/<org>; Domain=<domain>; Username=<username>; Password=<password>;

On Prem (Claims/IFD)

Url=https://<server>; Username=<username>; Password=<password>;

On Line (Windows Live)

Url=https://<org>.crm.dynamics.com; Username=<email>; Password=<password>; DeviceID=<DeviceId>; DevicePassword=<DevicePassword>;

On Line (O365)

Url=https://<org>.crm.dynamics.com; Username=<email>; Password=<password>;"

You can find a full list of connection string parameters in the SDK

You can then easily instantiate an OrganizationService using:

var connection = new CrmConnection("Xrm");
var service = new OrganizationService(connection);

If you want to simplify creation of the ServiceContext, and make it much easier to handle thread safety – you can use a configuration file that looks like:

<configuration>
<configSections>
    <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
</configSections>
<microsoft.xrm.client>
    <contexts>
        <add name="Xrm" type="Xrm.XrmServiceContext, Xrm" serviceName="Xrm"/>
    </contexts>
    <services>
        <add name="Xrm" type="Microsoft.Xrm.Client.Services.OrganizationService, Microsoft.Xrm.Client"
        instanceMode="[Static | PerName | PerRequest | PerInstance]"/>
    </services>
</microsoft.xrm.client>
</configuration>

You can then instantiate the default context simply by using:

var context = CrmConfigurationManager.CreateContext("Xrm") as XrmServiceContext;

The most interesting part of this is the instanceMode. It can be Static, PerName, PerRequest, PerInstance. By setting it to PerRequest, you will get a OrganizationService per ASP.NET request in a portal scenario – making your code more efficient and thread safe (provided you are not using asynchronous threads in ASP.NET).

The examples above I find are the most common configurations, although you can also specify multiple contexts with optional caching if required for specific contexts – the SDK has a full set of configuration file examples. Using the configuration file technique can simplify your code and ensure your code is thread safe.

On the topic of thread safety, it was recently brought to my attension that there appears to a bug with the ServiceConfigurationFactory such that if you are instantiating your OrganizationService passing the ServiceManager to the constructor in conjunction with EnableProxyTypes, you can occationally get a threading issue with the error "Collection was modified; enumeration operation may not execute". The work around to this is to ensure that the call to EnableProxyTypes is wrapped in the following:

if (proxy.ServiceConfiguration.CurrentServiceEndpoint.EndpointBehaviors.Count == 0)
{
    proxy.EnableProxyTypes();
}

More information can be found in my forum post.

Next up in this series are the Utility and extension functions in the Microsoft.Xrm.Client developer extensions.

@ScottDurow

 

Comments

Comments are closed