locked
CRM 2013 On-premises Plugin with Claims-based Authentication with early binding RRS feed

  • Question

  • Hi Im developing plugins to CRM 2013 On-premises with Claims-based Authentication.

    Im experiencing problems with using early-binding together with Claims-based authentication.

    I can generate the proxy classes with svcutil.exe but when I try to use them in my plugins I get this error:
    Exception: Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'Crm.Context.Account'.

    This is the plugin code that fails:

    var context = serviceProvider.GetPluginExecutionContext();
    var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    var service = serviceFactory.CreateOrganizationService(context.UserId);

    var targetEntity = context.InputParameters[ParameterName.Target] as Entity
    if (context.MessageName != MessageName.Create || targetEntity.LogicalName != Account.EntityLogicalName) return;
    using (var ctx = new ProxyContext(service))
    {
     var query = from a in ctx.AccountSet
                                     where a.AccountId.Equals(targetEntity.Id)
                                     select a;
                      
        var account = query.ToList().FirstOrDefault();
    }

    Any help would be greatly appreciated.


    • Edited by CM.EG Tuesday, March 4, 2014 8:26 AM
    Tuesday, March 4, 2014 8:25 AM

Answers

  • Info if someone else runs into this problem:

    This problem started after Claims-based authentication and IFD was configured, previously early binding with plugins was working fine.

    I found out that the following line of code solved the problem:

    serviceFactory.GetType().GetProperty("ProxyTypeAssembly").SetValue(serviceFactory, typeof(CrmContext).Assembly, null);

    Where CrmContext is the Proxy Classes generated with svutil.exe

    • Marked as answer by CM.EG Tuesday, March 4, 2014 6:31 PM
    Tuesday, March 4, 2014 6:31 PM

All replies

  • Info if someone else runs into this problem:

    This problem started after Claims-based authentication and IFD was configured, previously early binding with plugins was working fine.

    I found out that the following line of code solved the problem:

    serviceFactory.GetType().GetProperty("ProxyTypeAssembly").SetValue(serviceFactory, typeof(CrmContext).Assembly, null);

    Where CrmContext is the Proxy Classes generated with svutil.exe

    • Marked as answer by CM.EG Tuesday, March 4, 2014 6:31 PM
    Tuesday, March 4, 2014 6:31 PM
  • Note the solution dosnt work if you are running asynchronous processes:
    >Web Service Plug-in failed in SdkMessageProcessingStepId: {F9477660-A178-E311-9404-001DD8B71CC1}; EntityName: account; Stage: 30; MessageName: Update; AssemblyName:
    Microsoft.Crm.Extensibility.InternalOperationPlugin, Microsoft.Crm.ObjectModel, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;
    ClassName: Microsoft.Crm.Extensibility.InternalOperationPlugin; Exception: Unhandled Exception: System.Reflection.TargetInvocationException:
    Exception has been thrown by the target of an invocation.

    Cause: You cant use reflection on internal Microsoft plugins.

    So Seems like I'm forced to use late binding on custom workflows and async plugins.

    If anyone has a solution to this please let me know.

    Tuesday, March 4, 2014 8:46 PM