The views and opinions expressed in this blog are those solely of the author(s) and do not necessarily reflect Microsoft’s current policy, position, or branding. For official announcements and guidance on Dynamics 365 apps and services, please visit the Microsoft Dynamics 365 Blog.
Personalized Community is here!
Quickly customize your community to find the content you seek.
Choose your path Increase your proficiency with the Dynamics 365 applications that you already use and learn more about the apps that interest you. Up your game with a learning path tailored to today's Dynamics 365 masterminds and designed to prepare you for industry-recognized Microsoft certifications.
Visit Microsoft Learn
2020 Release Wave 2Discover the latest updates and new features to Dynamics 365 planned through March 2021.
Release overview guides and videos Release Plan | Preview 2020 Release Wave 2 TimelineWatch the 2020 Release Wave 1 virtual launch event
Ace your Dynamics 365 deployment with packaged services delivered by expert consultants. | Explore service offerings
Connect with the ISV success team on the latest roadmap, developer tool for AppSource certification, and ISV community engagements | ISV self-service portal
The FastTrack program is designed to help you accelerate your Dynamics 365 deployment with confidence.
FastTrack Program | Finance TechTalks | Customer Engagement TechTalks | Upcoming TechTalks
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.
Business Applications communities