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:
<add name="Xrm" connectionString="Url=http://<server>/<org>"/>
The Connection string can take on the following forms:
On Prem (Windows Auth)
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:
<section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
<add name="Xrm" type="Xrm.XrmServiceContext, Xrm" serviceName="Xrm"/>
<add name="Xrm" type="Microsoft.Xrm.Client.Services.OrganizationService, Microsoft.Xrm.Client"
instanceMode="[Static | PerName | PerRequest | PerInstance]"/>
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)
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.