WCF is stateless by nature and there is no support for global variables or caching out of the box. However, You can use Caching Application Block to create and access global variables that you might need in your WCF and also to improve performance. View this Power Point Presentation to get acquainted with Caching application block if you need to, otherwise read on.
In this article I am going to discuss how to use the Caching Application Block with WCF and I will show the details and the step by step instructions to add caching to your WCF. The diagram below is a block diagram of the pieces involved
This diagram shows a WCF service running in ASPNetCompatibilityMode. When the WCF starts it will load the cache proactively with look up data. when the look up data expire cache events will fire in order to load the new data. so let's begin.
Caching application block support two modes of caching those are "Proactive" and "Reactive".
In Proactive Caching data is loaded even before any of your WCF operations is called. This is particularly important for look up data because you want your look up data to be loaded and available to all the WCF operations at the start.
In Reactive Caching on the other hand, data is loaded into the cache via the WCF operations themselves.
To use caching application block and load data proactively you can follow the steps below.
1- Download the enterprise library from Microsoft website.
2- Create your WCF and make it support AspNetCompatibilityRequirements. (more on this here). To do so, decorate your WCF class (not the interface) with the following attribute
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
Then add this entry to your web.config inside the node system.serviceModel
3- Nowthat your WCF supports AspNetCompatibilityRequirements, then you can use global.asax. Add global.asax to your project and in the application_start load your cache.
4- Here is the code to load your cache
ICacheManager _wcfCache = CacheFactory.GetCacheManager();
_wcfCache.Add("table1", table1, CacheItemPriority.Normal, new RefreshTable1(), new ExtendedFormatTime("* * * * *"));
The first line will declare and load cache object and the second line will add an entry in the cache. This entry will be refreshed every minute using the ExtendedFormatTime expiration task
new ExtendedFormatTime("* * * * *")
and when refreshed the refresh method in a class called RefreshTable1 will be called to reload table1
The caching application block is also instrumented and that mean you can use PerfMon.exe to see how your cache is doing.
If you are new to performance counters watch this video to get your feet wet in custom counters and read this about performance counters. Here is also a sample application. The caching application block provides the following counters. Read More
Total Cache Misses - NumberOfItems64
Total Cache Hits - NumberOfItems64
Total Cache Expiries - NumberOfItems64
Total Cache Scavenged Items - NumberOfItems64
Total Cache Entries - NumberOfItems64
Total Updated Entries - NumberOfItems64
Total # of Cache Access Attempts - RawBase
Updated Entries/sec - RateOfCountsPerSecond32
Cache Scavenged Items/sec - RateOfCountsPerSecond32
Cache Expiries/sec - RateOfCountsPerSecond32
Cache Hits/sec - RateOfCountsPerSecond32
Cache Misses/sec - RateOfCountsPerSecond32
Cache Hits/sec - RateOfCountsPerSecond32
Cache Misses/sec - RateOfCountsPerSecond32
Cache Hit Ratio - RawFraction
Total Cache Hits - NumberOfItems64
Total Cache Expiries - NumberOfItems64
Total Cache Scavenged Items - NumberOfItems64
Total Cache Entries - NumberOfItems64
Total Updated Entries - NumberOfItems64
Total # of Cache Access Attempts - RawBase
Updated Entries/sec - RateOfCountsPerSecond32
Cache Scavenged Items/sec - RateOfCountsPerSecond32
Cache Expiries/sec - RateOfCountsPerSecond32
Cache Hits/sec - RateOfCountsPerSecond32
Cache Misses/sec - RateOfCountsPerSecond32
Cache Hits/sec - RateOfCountsPerSecond32
Cache Misses/sec - RateOfCountsPerSecond32
Cache Hit Ratio - RawFraction
In order to use PerfMon to see your run time cache performance you will need to use the server explorer inside your visual studio and add the keys above as displayed in the screen shots below.
After adding the instrumentation keys, you need to run PerfMon.exe and add the keys to the monitor as per the screen shots below
Then enjoy watching the performance of your cache as follows.