Ms CRM 2011 == Configure the Context with the Configuration File

Configure the Context with the Configuration File

When using Developer Extensions for Microsoft Dynamics CRM 2011, 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. The following shows part of the of default configuration file:

XML

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

C#

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.

C#

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.

XML

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

XML

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

XML

<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”, andobjectCacheName=”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.

C#

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:

XML

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

Cheers,

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s