MemoryCache and multiple per call WCF services

c#, caching, iis, memorycache, wcf

Solution

`1.the services are all hosted in the same app in IIS`

the answer is yes if you are using `MemoryCache.Default` as your default cache object From MSDN

`This property always returns a reference to the default cache instance. For typical application scenarios, only one instance of MemoryCache is required.`

you could use it like the following

ObjectCache cache = MemoryCache.Default;

Is it possible to configure it in the following way

<system.runtime.caching>
 <memoryCache>
  <namedCaches>
   <add name="Default" physicalMemoryLimitPercentage="20"/>
  </namedCaches>
 </memoryCache>
</system.runtime.caching>

from your others services instance you can access your memory cache like the following

List<string> cacheKeys = MemoryCache.Default.Select(kvp => kvp.Key).ToList();

foreach (string cacheKey in cacheKeys)
          MemoryCache.Default.Remove(cacheKey); 

`2.the services are hosted in different IIS applications on the same server`

this will be a bit tricky but it will remains a valid option you can create a dedicated webservice for caching that can be used by others webservices using the `netnamedPipeBinding` given that are on the same server

Problem

Is using the `MemoryCache` class a valid option if I want the cached data to be visible across multiple WCF services (with PerCall instance mode)? There are two cases: - the services are all hosted in the same app in IIS - the services are hosted in different IIS applications on the same server

Original source