CRM 2011 Plugin And Early Bound Entities

By | March 16, 2012

In my first crm 2011 project I struggled a little bit with the early binding functionality. When you generate the entities with CrmSvcUtil and place the entities in the plugin project everything works fine. But when you seperate the entities from you plugin logic in a other dll, early binding won’t work.
The first solution I found in the internet was this here. Referencing following libraries in the plugin project:

Microsoft.Crm.ObjectModel.dll
Microsoft.Crm.Platform.Server.dll

And then setting the ProxyTypesAssembly attribute.

(Microsoft.Crm.Extensibility.PipelineExecutionContext)context).ProxyTypesAssembly = typeof(AnyGeneratedEntity).Assembly;

This works fine. But when your plugin runs asynchronous the code above will fail. Because the context isn’t a PipelineExecutionContext object. Context in async plugins are of type AsyncExecutionContext. And this class is placed in the AsyncCrmService.exe (yes! EXE). This means, you have to reference this exe file and cast the context to set the ProxyTypesAssembly.

(Microsoft.Crm.......AsyncExecutionContext)context).ProxyTypesAssembly = typeof(AnyGeneratedEntity).Assembly;

A other solution is to set the ProxyTypesAssembly with reflection. It works but isn’t really a nice solution too.

Both solutions are not very nice. Because you have to reference CRM 2011 dlls,exes which are not really meant for creating plugins, or you have to develop type unsafe.


My solution is
Afer inspecting the AsyncExecutionContext class with ILSpy I found out, that every context implements the interface IProxyTypesAssemblyProvider. And this interface is placed in the xrm sdk 😉

Now, the solution is simple. Remove any CRM 2011 dlls,exes like ObjectModel, Platform.Server or CrmAsyncService.exe and set the ProxyTypesAssembly property like here:

(IProxyTypesAssemblyProvider)context).ProxyTypesAssembly = typeof(AnyGeneratedEntity).Assembly;

(The interface is placed in the xrm sdk dll which you need for plugin development.)

This will work for every other context which supports early binding.