I was recently putting together a distributed application cache proof of concept for a customer that leveraged the Windows Server AppFabric Cache.  In order to take advantage of this functionality, my project needed to reference the following assemblies:

  • Microsoft.ApplicationServer.Caching.Client
  • Microsoft.ApplicationServer.Caching.Core

Normally this would be easy enough.  Assuming these are system level assemblies I’d just reference them from the GAC - and sure enough I was able to find them there.  Unfortunately, that was a mistake.  It turns out that I also had the Windows Azure SDK installed on my development machine.  Windows Azure also supports the AppFabric Cache, but with a different version of these same named assemblies which are installed in the GAC.  So when I ran my code and attempted to create my DataCacheFactory, I ended up with the following exception stack:

  • {"'The invocation of the constructor on type 'SampleCache.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'."}
  • {"The type initializer for 'Microsoft.ApplicationServer.Caching.DataCacheFactoryConfiguration' threw an exception."}
  • {"[A]Microsoft.ApplicationServer.Caching.DataCacheClientSection cannot be cast to [B]Microsoft.ApplicationServer.Caching.DataCacheClientSection. Type A originates from 'Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\\Windows\\assembly\\GAC_MSIL\\Microsoft.ApplicationServer.Caching.Core\\1.0.0.0__31bf3856ad364e35\\Microsoft.ApplicationServer.Caching.Core.dll'. Type B originates from 'Microsoft.ApplicationServer.Caching.Core, Version=101.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\\Users\\jschaffe\\Documents\\Visual Studio 2010\\Projects\\AppFabric\\Caching\\SampleCache\\SampleCache\\bin\\Debug\\Microsoft.ApplicationServer.Caching.Core.dll'."}

The final exception in the stack indicates the problem.

{"[A]Microsoft.ApplicationServer.Caching.DataCacheClientSection cannot be cast to [B]Microsoft.ApplicationServer.Caching.DataCacheClientSection. Type A originates from 'Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\\Windows\\assembly\\GAC_MSIL\\Microsoft.ApplicationServer.Caching.Core\\1.0.0.0__31bf3856ad364e35\\Microsoft.ApplicationServer.Caching.Core.dll'. Type B originates from 'Microsoft.ApplicationServer.Caching.Core, Version=101.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\\Users\\jschaffe\\Documents\\Visual Studio 2010\\Projects\\AppFabric\\Caching\\SampleCache\\SampleCache\\bin\\Debug\\Microsoft.ApplicationServer.Caching.Core.dll'."}

Notice the difference in the Version numbers.  One is 1.0.0.0 (Windows Server AppFabric Cache) while the other is 101.0.0.0 (Windows Azure AppFabric Cache).  The incompatibility derives from the combination of the configuration section in my app.config (shown below) that I need in order to utilize the Windows Server AppFabric Cache and my reference to the Windows Azure AppFabric Cache assemblies that I referenced earlier.

    <section name="dataCacheClient" 
             type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, 
                   Microsoft.ApplicationServer.Caching.Core, 
                   Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"         
             allowLocation="true"         
             allowDefinition="Everywhere"/>

So the root problem is that I’m trying to assign an instance of the DataCacheClientSection from the Windows Azure AppFabric Cache assembly to an instance of the same type from the Windows Server AppFabric Cache assembly. 

To correct the problem I need to reference the proper version of the assemblies.  Depending on whether I’m developing for 32-bit or 64-bit, I reference the assemblies from the following directories:

  • 32-bit systems - .\Windows\System32\AppFabric directory
  • 64-bit systems - On 64-bit operating systems, the AppFabric directory will not be directly visible. To work around this problem, replace the System32 directory name with the name SysNative. This results in navigating to the C:\Windows\SysNative\AppFabric directory in this step

Once the references are updated, the application runs without error.