On Thanksgiving day while I was running in the snow I had an idea – what if you could create an Operation Behavior that would allow WCF to cache the result of a service call in Windows Server AppFabric Cache?
Download WCF AppFabric Caching Caching Sample Behavior
So I created a sample application that does this by implementing an attribute that implements IOperationBehavior and IOperationInvoker along with a Service Behavior that supports AppFabric Caching. I wanted to make this work with AppFabric Caching but not require you to to have AppFabric caching running when developing or testing the service. The solution I came up with was to look for a Service Extension that implements IServiceCache. If I find it, I use it. Otherwise I create a local cache using System.Runtime.MemoryCache.
To ask for caching on a service you can either add it to your contract or request it in config.
public interface ISampleService
SampleData GetSampleData(SampleDataRequest request);
To request caching in your config
<add type="Microsoft.ServiceModel.Samples.Caching.AppFabric.AppFabricCacheElement, Microsoft.ServiceModel.Samples.Caching.AppFabric, Version=184.108.40.206, Culture=neutral, PublicKeyToken=null" name="appFabricCachingBehavior" />
<servicemetadata httpgetenabled="true" />
<servicedebug includeexceptiondetailinfaults="true" />
<servicehostingenvironment multiplesitebindingsenabled="true" />
The sample includes unit tests that demonstrate how you can setup parameters for cache keys and complex types. Hope you like it!