Configure the context with the configuration file (Dynamics CRM 2015)

 

Applies To: Dynamics CRM 2015

When using Developer Extensions forMicrosoft Dynamics CRM 2015, rather than programmatically adjusting the dependency composition, it is possible to influence the default object types by modifying the application configuration file. The minimum configuration provides just the connection string and data context. The default dependency objects are created under the hood just as in the programmatic case.

In This Topic

Overview

Configuration rules

Specify the instanceMode attribute

Default configuration

Overview

The following shows part of the of default configuration file:

<configuration>

 <configSections>
  <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
 </configSections>

 <connectionStrings>
  <add name="Xrm" connectionString="Url=http://crm.contoso.com/xrmContoso"/>
 </connectionStrings>

 <microsoft.xrm.client>
  <contexts>
   <add name="Xrm" type="Xrm.XrmServiceContext, Xrm"/>
  </contexts>
 </microsoft.xrm.client>

</configuration>

The CrmConfigurationManager is used to surface the configuration into actual code. The CreateContext method takes the name of the context configuration element that should be used to instantiate the data context.

var contextName = "Xrm";
using (var context = CrmConfigurationManager.CreateContext(contextName) as XrmServiceContext)
{
}

Passing null takes the default context configuration element, which is the first element in the collection. In addition, it is possible to override this with an explicit default element. The following code obtains the same results as shown in the preceding example.

using (var context = CrmConfigurationManager.CreateContext() as XrmServiceContext)
{
}

The context configuration element is responsible for specifying the connection string and, by default, it looks for a connection string name with the same name as the context configuration element name. The above example matches on the name "Xrm". The connection string name can be specified explicitly to highlight and isolate this relationship, as shown here.

<connectionStrings>
 <add name="MyCRM" connectionString="Url=http://crm.contoso.com/xrmContoso"/>
</connectionStrings>

<microsoft.xrm.client>
 <contexts>
  <add name="Xrm" type="Xrm.XrmServiceContext, Xrm" connectionStringName="MyCRM"/>
 </contexts>
</microsoft.xrm.client>

The nested dependencies can be specified and linked together, as shown in this example:

<microsoft.xrm.client>
 <contexts>
  <add name="Xrm" type="Xrm.XrmServiceContext, Xrm" serviceName="MyService"/>
 </contexts>
 <services>
  <add name="MyService" type="Microsoft.Xrm.Client.Services.CachedOrganizationService, Microsoft.Xrm.Client" serviceCacheName="MyServiceCache"/>
 </services>
 <serviceCache>
  <add name="MyServiceCache" type="Microsoft.Xrm.Client.Services.OrganizationServiceCache, Microsoft.Xrm.Client" objectCacheName="MyObjectCache"/>
 </serviceCache>
 <objectCache>
  <add name="MyObjectCache" type="System.Runtime.Caching.MemoryCache, System.Runtime.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
 </objectCache>
</microsoft.xrm.client>

The following shows how to disable caching:

<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"/>
  </services>
</microsoft.xrm.client>

Configuration rules

The following lists the rules for configuring the context.

  • Specify the <context> element - A default type class does not exist for the OrganizationServiceContext dependency so specifying the <context/> element is required, unless you are working solely with the dynamic CrmOrganizationServiceContext and without code generation.

  • Optionally specify the dependency name - The default dependency name attribute (such as name="Xrm") is optional. When omitted, the first <add/> element under the configuration collection is the default.

  • Specify the linking attribute – It is common to specify the linking attribute such as serviceName="Xrm", serviceCacheName="Xrm", and objectCacheName="Xrm". However, when a linking attribute is omitted, the CrmConfigurationManager tries to link to the sub-dependency with the same name as the parent dependency.

Any of the dependencies from the configuration can be instantiated with the CrmConfigurationManager along with the appropriate static method. The resulting object can be casted to the known type or referenced as the base type without casting.

var name = "Xrm";
var connection = new CrmConnection(name);
var objectCache = CrmConfigurationManager.CreateObjectCache(name) as MemoryCache;
var serviceCache = CrmConfigurationManager.CreateServiceCache(name, connection) as OrganizationServiceCache;
var service = CrmConfigurationManager.CreateService(connection, name) as CachedOrganizationService;
var context = CrmConfigurationManager.CreateContext(name) as XrmServiceContext;

Specify the instanceMode attribute

If a configuration element specifies an InstanceMode attribute, the CrmConfigurationManager will adjust its instantiation behavior accordingly.

  • Static – always returns the same static instance.

  • PerName– returns a single instance for each configuration element specified.

  • PerRequest – returns the same first instance in the context of a Web request, for example. one instance for each HttpContext instance.

  • PerInstance – returns a new instance on each call.

Default configuration

The full configuration with default values:

<configuration>

 <configSections>
  <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
 </configSections>

 <connectionStrings>
  <add name="Xrm" connectionString="ServiceUri=...; Domain=...; Username=...; Password=..."/>
 </connectionStrings>

 <microsoft.xrm.client>
  <contexts default="Xrm">
   <add name="Xrm" type="Xrm.XrmServiceContext, Xrm" connectionStringName="Xrm" serviceName="Xrm"/>
  </contexts>
  <services default="Xrm">
   <add
    name="Xrm"
    type="Microsoft.Xrm.Client.Services.CachedOrganizationService, Microsoft.Xrm.Client"
    serviceCacheName="Xrm"
    instanceMode="PerRequest" [Static | PerName | PerRequest | PerInstance]
   />
  </services>
  <serviceCache default="Xrm">
   <add
    name="Xrm"
    type="Microsoft.Xrm.Client.Services.OrganizationServiceCache, Microsoft.Xrm.Client"
    objectCacheName="Xrm"
    cacheMode="LookupAndInsert" [LookupAndInsert | InsertOnly | Disabled]
    returnMode="Cloned" [Shared | Cloned]
    queryHashingEnabled="false" [false | true]
   />
  </serviceCache>
  <objectCache default="Xrm">
   <add
    name="Xrm"
    type="System.Runtime.Caching.MemoryCache, System.Runtime.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    instanceMode="PerName" [Static | PerName | PerInstance]
    absoluteExpiration=""
    slidingExpiration="00:00:00" [HH:MM:SS]
    duration="00:00:00" [HH:MM:SS]
    priority="Default" [Default | NotRemovable]
    outputCacheProfileName="Xrm"
   />
  </objectCache>
 </microsoft.xrm.client>

 <system.runtime.caching>
  <memoryCache>
   <namedCaches>
    <add name="Xrm"
     cacheMemoryLimitMegabytes="0"
     physicalMemoryLimitPercentage="0"
     pollingInterval="00:00:00" />
    </namedCaches>
   </memoryCache>
 </system.runtime.caching>

 <system.web>
  <caching>
   <outputCacheSettings>
    <outputCacheProfiles>
     <add name="Xrm" enabled="true" duration="-1"/>
    </outputCacheProfiles>
   </outputCacheSettings>
  </caching>
 </system.web>

</configuration>

See Also

Developer extensions context object model (Dynamics CRM 2015)
Access entity relationships (Dynamics CRM 2015)
Attach entities to the context (Dynamics CRM 2015)

© 2016 Microsoft. All rights reserved. Copyright