Search code examples
pluginsdynamics-crmcrmmicrosoft-dynamics

Unable to cast object of type 'Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper' to type 'Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy'


Could anyone help me with this error? It is happening in a Microsoft Dynamics CRM 2015 Online Plugin.

It's a late binding implementation...

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unable to cast object of type 'Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper' to type 'Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy'.Detail:
-2147220891 OperationStatus 0 SubErrorCode -2146233088 Unable to cast object of type 'Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper' to type 'Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy'.
2015-03-25T19:31:18.5408758Z

[JHSF.CRM.Principal: JHSF.CRM.Principal.Application.Plugin.PluginAdesaoFidelidade] [dd59ba6a-7ad1-e411-80de-c4346bb59e2c: JHSF.CRM.Principal.Application.Plugin.PluginAdesaoFidelidade: Create of jhsf_adesao_programa_fidelidade]

Error : Unable to cast object of type 'Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper' to type 'Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy'. StackTrace:
at SDKore.Xrm.CrmServiceProvider..ctor(String OrganizationName, Nullable`1 IsOffline, Object Provider)


Solution

  • Preferably, you should not be referencing Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy in a plug-in. This is, as the word Client indicates, for client-side not server-side code.

    That said, you cannot make the cast you reference above and I don't think you can create an OrganizationServiceProxy in a sandboxed plugin. If you are using early-bound entities you can create an OrganizationServiceContext - from the client DLL - by passing in the instance of IOrganizationService available to your plugin.