locked
Unable to cast object of type 'Microsoft.Crm.Sdk.Moniker' to type 'Microsoft.Xrm.Sdk.EntityReference'. RRS feed

  • Question

  • Hi,

    I have a plugin which is registered on the retrieve, synchronously . When I perform the action that triggers the plugin I keep getting the error # Unable to cast object of type 'Microsoft.Crm.Sdk.Moniker' to type 'Microsoft.Xrm.Sdk.EntityReference'. I have done a trace on the server and got the exact plugin, debugged the plugin on a dev environment but not hitting the issue, it all works fine (I have build the plugin and updated the dll on the server that has the issue without success, the erro is still present). The plugin rightfully expects an EntityReference and I check that the Target is an EntityReference. The version of crm is Dynamics CRM 2011. Can anyone assist ? Thanks.


    • Edited by JackieSoni Friday, March 22, 2013 11:39 AM
    Friday, March 22, 2013 11:32 AM

All replies

  • Hi,

    Sometimes you need to do an iisreset on the server to get the updated plugin in use, it seems to cache the old dll somehow. If that doesn't work perhaps you can share the code to see if there's something missing somehow.

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Friday, March 22, 2013 11:52 AM
  • Hi Rickard,

    Thanks for the prompt response. Yes, I did the IISRESET after the assembly update but the error is still displayed . The code is in use for auditing and I only require the logicalname and id to right to another db eslewhere, nothing complex in this class. That's the reason why I am out of options.... I aslo tried adding config to the web.config file 

                          

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Crm.Sdk" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="4.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>

    Code on the plugin that's causing issue:

    protected override IEnumerable<IEnumerable<string>> GetAuditRows(IPluginExecutionContext context)
            {
                if (context.MessageName != "Retrieve")
                {
                    throw new Exception("Must be registered on Retrieve");
                }

              if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference) {  

                   var target =    (EntityReference)context.InputParameters["Target"];

               yield return new[] { target.LogicalName, target.Id.ToString() };

              }

    }

    Friday, March 22, 2013 2:08 PM
  • Hi,

    Its worth checking the field types are same in both enviornments. By looking at the error it is pointing that some parsing needs to be done.

    Friday, March 22, 2013 2:21 PM
  • Microsoft.Crm.Sdk.Moniker is a CRM 4 type  - have you upgraded from a CRM 4 environment?

    Why are you adding the assembly redirect?

    If CRM 2011 thinks that your plugin is a CRM 4 plugin, it will pass CRM 4 types to it to ensure backward compatibility.


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Friday, March 22, 2013 2:57 PM
    Answerer
  • Hi Shaffana,

    Thanks. Unfortunately, the field types are the ok.  

    Friday, March 22, 2013 3:23 PM
  • Hi Scott,

    Thanks. The environment that has the error popping up was not upgraded. It was a fresh CRM 2011 installation. I build and registered the latest dll but still had the same issue. 

    Friday, March 22, 2013 3:29 PM
  • Hi,

    I'm still confused as to why you are adding the redirect code - you shouldn't be using the Microsoft.Crm.Sdk assembly at all - since this is from CRM4 - you should use the Microsoft.Xrm.Sdk assembly.

    hth


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Friday, March 22, 2013 6:23 PM
    Answerer
  • Could you share more of your plugin code with us, as well as a complete stack trace?

    Is your code thread safe? Are you storing objects in fields of your plugin class?

    When a plugin adds attribute parameters to a Target entity with the wrong type (e.g. Moniker in stead of EntityReference vice versa), this plugin itself will not raise exceptions. However, some steps further in the plugin pipeline the CRM platform will run into an error because it fails to process faulty parameter. Could this be the case with your code? The CRM trace log could give you more information on this error.


    If this post is an answer or helpful, please do not forget to vote!

    Monday, March 25, 2013 8:43 AM
  • Hi Scott,

    I will omit the config redirect and see what happens, thanks. I'll let you know the out come.

    Monday, March 25, 2013 12:08 PM
  •  

    Hi Henk,

    Thanks for the response, the trace log as requested below,  I'll check other classes and post an update:

    Process: w3wp |Organization:1c4870b4-b78e-e111-905c-005056bc6944 |Thread:   25 |Category: Platform.Sdk |User: e0d7d1da-2a28-e211-a1c9-005056bc614c |Level: Error | VersionedPluginProxyStepBase.Execute
    >Web Service Plug-in failed in SdkMessageProcessingStepId: {63F39DB5-BE6D-E111-99C4-00155D50F142}; EntityName: contact; Stage: 40; MessageName: Retrieve; AssemblyName: AuditPlugins.Retrieve, AuditPlugins, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fc04268fc004b769; ClassName: AuditPlugins.Retrieve; Exception: Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'Microsoft.Crm.Sdk.Moniker' to type 'Microsoft.Xrm.Sdk.EntityReference'.
       at AuditPlugins.Retrieve.<GetAuditRows>d__0.MoveNext()
       at AuditPlugins.AuditPluginBase.WriteAudit(IPluginExecutionContext context, IOrganizationServiceFactory serviceFactory, String connectionString, IEnumerable`1 rows, String userName)
       at AuditPlugins.AuditPluginBase.ExecutePlugin(IServiceProvider serviceProvider, IPluginExecutionContext context, IOrganizationServiceFactory organizationServiceFactory)
       at xxx.Xrm.Framework.Plugin.BasePlugin.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context)
       at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)

    Monday, March 25, 2013 12:12 PM
  • It does look like you've built your plugin against the CRM 4 assemblies as well as the CRM2011 ones. Could you post your Plugin Code and list of project references in the plugin Project?

    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Monday, March 25, 2013 12:18 PM
    Answerer