Developer extensions context object model (Dynamics CRM 2013)

 

Applies To: Dynamics CRM 2013

Developer Extensions for Microsoft Dynamics CRM 2013 provides a set of inter-dependent classes that minimize the work needed to get up and running. The most basic program only requires the connection string (or the name of a connection string in the app.config) and a data context created by CrmSvcUtil.exe as shown in the following example:

var connection = CrmConnection.Parse("Url=http://crm.contoso.com/xrmContoso");

using (var context = new XrmServiceContext(connection))
{
var accounts = context.AccountSet;

foreach (var account in accounts)
Console.WriteLine(account.Name);
}

Under the hood there are nested objects that handle lower level operations. The data context is the composition of all of these objects and, therefore, the behavior of the data context is dependent on the objects. These dependencies can be exposed through use of specific constructors. When expanded, the default object composition looks like the following example:

var myConnection = CrmConnection.Parse("Url=http://crm.contoso.com/xrmContoso");
var myObjectCache = MemoryCache.Default;
var myServiceCache = new OrganizationServiceCache(myObjectCache, myConnection);
using (var myService = new CachedOrganizationService(myConnection, myServiceCache))
using (var myContext = new XrmServiceContext(myService))
{
}

Dependencies

The following table lists available dependencies and their behaviors.

Interface

Base Class

Default Type

Description

Configuration Element

CrmConnection

CrmConnection

The connection details in the form of a connection string.

<connectionStrings>

ObjectCache

MemoryCache

The raw data caching service that is independent of the other components of the SDK.

<objectCache>

IOrganizationServiceCache

OrganizationServiceCache

OrganizationServiceCache

A further abstracted data caching service that caches the results of IOrganizationService operations. Automatically sets up cache item dependencies and performing update operations invalidates dependent cache items.

<serviceCache>

IOrganizationService

OrganizationService

CachedOrganizationService

A wrapper class around the IOrganizationService that manages the details of setting up an OrganizationServiceProxy or other arbitrary IOrganizationService objects. When paired with an OrganizationServiceCache object, becomes a cached service.

<services>

OrganizationServiceContext

The OData and LINQ capable data context. Needs to be specifically declared or configured so no default type exists.

<contexts>

IDiscoveryService

DiscoveryService

A wrapper class around the IDiscoveryService that manages the details of setting up a DiscoveryServiceProxy or other arbitrary IDiscoveryService objects. No caching implementations exist for this and cannot be configured in app.config.

Implementing the interfaces, inheriting the available base classes, or simply rearranging the object nesting can be done to modify the final behavior. For example, one way to disable caching functionality is to omit the cache related objects and prefer the raw base classes.

var myConnection = CrmConnection.Parse("Url=http://crm.contoso.com/xrmContoso");
using (var myService = new OrganizationService(myConnection))
using (var myContext = new XrmServiceContext(myService))
{
}

To remove the dependency on code generated by CrmSvcUtil.exe in favor of dynamic code, use the CrmOrganizationServiceContext class. Another scenario is to omit the data context entirely and just invoke the IOrganizationService directly. This is also the approach to invoking the IDiscoveryService.

var myConnection = CrmConnection.Parse("Url=http://crm.contoso.com/xrmContoso");
using (var myContext = new CrmOrganizationServiceContext(myConnection))
{
}
using (var myService = new OrganizationService(myConnection))
{
var response = myService.Execute(new WhoAmIRequest()));
}
var discoveryConnection = CrmConnection.Parse("Url=http://crm.contoso.com");
using (var myDiscoveryService = new DiscoveryService(discoveryConnection))
{
var response = myDiscoveryService.Execute(new RetrieveOrganizationsRequest()));
}

Context enhancements

The most significant difference between the CrmOrganizationServiceContext, which is the direct base class of XrmServiceContext, of Developer Extensions for Microsoft Dynamics CRM and the base OrganizationServiceContext in the core SDK, is the implementation of the IUpdatable and IExpandProvider interfaces. This custom implementation allows the generated data context and the entity objects produced by the data context to participate in the WCF Data Services framework. To serve the data context as an OData endpoint, add a new WCF Data Service endpoint (.svc) to an existing Web application.

<%@ ServiceHost Language="C#" Factory="System.Data.Services.DataServiceHostFactory, System.Data.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Service="MySite.MyContext" %>

Here is an example service endpoint code-behind (.svc.cs) that can be used for development. Specify the XrmServiceContext as the generic type parameter of the DataService<T> class.

using System.Data.Services;
using System.Data.Services.Common;
using System.ServiceModel;
using Xrm;

namespace MySite
{
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyContext : DataService<XrmServiceContext>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: Set rules to indicate which entity sets and service operations are visible, updatable, etc.
config.UseVerboseErrors = true;
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
}

See Also

Developer extensions for Microsoft Dynamics CRM 2013
Configure the context with the configuration file (Dynamics CRM 2013)
Access entity relationships (Dynamics CRM 2013)
Attach entities to the context (Dynamics CRM 2013)
Portal walkthroughs (Dynamics CRM 2013)