Developer extensions context object model (Dynamics CRM 2015)


Applies To: Dynamics CRM 2015

Developer Extensions for Microsoft Dynamics CRM 2015 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=");

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

foreach (var account in accounts)

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=");
var myObjectCache = MemoryCache.Default;
var myServiceCache = new OrganizationServiceCache(myObjectCache, myConnection);
using (var myService = new CachedOrganizationService(myConnection, myServiceCache))
using (var myContext = new XrmServiceContext(myService))


The following table lists available dependencies and their behaviors.


Base Class

Default Type


Configuration Element



The connection details in the form of a connection string.




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





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.





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.



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




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=");
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=");
using (var myContext = new CrmOrganizationServiceContext(myConnection))
using (var myService = new OrganizationService(myConnection))
var response = myService.Execute(new WhoAmIRequest()));
var discoveryConnection = CrmConnection.Parse("Url=");
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=, 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)
// 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 2015
Configure the context with the configuration file (Dynamics CRM 2015)
Access entity relationships (Dynamics CRM 2015)
Attach entities to the context (Dynamics CRM 2015)
Portal walkthroughs for Dynamics CRM 2015

© 2016 Microsoft. All rights reserved. Copyright