InvalidCastException when using MemoryCache in Custom CRM Workflow Activity

I have been struggling with the following error all morning.

I have a custom workflow activity in CRM 2011, that generates a PDF summary from CRM records. To maintain a reasonable good performance and flexibility, I have based the layout on a XML web ressource, where the layout configurations and images are cached using MemoryCache.

The code runs perfectly in Unit Test, but when running the actual workflow, I get the following error:

Workflow paused due to error: Unhandled Exception: System.InvalidCastException: [A]System.Collections.Generic.List`1[Crm.Pdf.PdfGenerator+AttributeDescription] cannot be cast to [B]System.Collections.Generic.List`1[Crm.Pdf.PdfGenerator+AttributeDescription]. Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
...
...
at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Explanation

It turned out the error occurs when caching types that are defined in the workflow assembly. CRM workflows are versioned, in the sence that running workflows will not update their implementation even if the actual workflow assembly is updated through plugin registration Tool. In this way, each time a workflow is writing to the MemoryCache, a new instance of the workflow assembly is being used. This causes a problem when fetching data from MemoryCache, because in order to cast the data back to the original type, not only must it be the same type and assembly, but also the same instance of the assembly.

Resolution

In order to make caching work in this scenario, there are two options:

  • Rewrite your caching code to store general types, for example by doing serialization
  • Add the custom types that needs caching to a separate assembly that can be stored in the Global Assembly Cache

I found the first option most preferable in my specific case, from a maintenance and deployment perspective.

Advertisements

One thought on “InvalidCastException when using MemoryCache in Custom CRM Workflow Activity

  1. Pingback: The Art of CRM Batch Jobs | Peder Møller Wagner's Code Lab

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s