6

We are trying to use early bound types in a CRM2011 plugin. To enable this it appears we need to either add a ProxyTypesBeavior(), or call EnableProxyTypes(). However, both of these properties apply to an OrganizationServiceProxy class, and do not exist on the IOrganizationService interface.

So if we are using the following code to get the organization service, how are we meant to obtain a proxy class to set the above properties on?

var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
shytikov
  • 9,155
  • 8
  • 56
  • 103
Matt
  • 1,370
  • 2
  • 16
  • 40

3 Answers3

4

For those of you using CRM Online, the reflection solution won't work since you're stuck in sandbox mode.

The following solution using the IProxyTypesAssemblyProvider interface (suggested by Pavel Korsukov) worked for me (source).

var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

var proxyTypesProvider = factory as IProxyTypesAssemblyProvider;
if (proxyTypesProvider != null)
{
    proxyTypesProvider.ProxyTypesAssembly = typeof(Xrm.XrmServiceContext).Assembly;
}
// Use the factory to generate the Organization Service.
var service = factory.CreateOrganizationService(context.UserId);
Jon Grant
  • 11,369
  • 2
  • 37
  • 58
Tom Faltesek
  • 2,768
  • 1
  • 19
  • 30
2

Guil on this thread offered an option to use reflection to bind the code gen proxy types to the service factory. And it worked for me. Won't be able to register it in sandbox, as reflection needs full trust.

 factory.GetType().GetProperty("ProxyTypesAssembly").SetValue(factory, typeof(YourCrmContext).Assembly, null);

http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/bc7e93d4-1b36-4e21-9449-f51b67a2e52c/

Jian Sun
  • 21
  • 2
-2

Write like this,

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
  • 2
    This is not an answer, it is just a suggestion to use explicit types rather than inferred types. This is a stylistic preference, and it certainly isn't wrong to use var! – Stephen Holt Dec 19 '12 at 11:20