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:
This allows you to cache responses from your WCF Web HTTP service operations. When a user sends an HTTP GET to your service that is configured for caching, ASP.NET sends back the cached response and the service method is not called.
WCF Per-Call Service. When WCF service is configured for Per-Call instance mode, Service instance will be created for each client request. This Service instance will be disposed after response is sent back to client. Following diagram represent the process of handling the request from client using Per-Call instance mode.
When WCF service is configured for Per-Call instance mode, Service instance will be created for each client request. This Service instance will be disposed after response is sent back to client. Following diagram represent the process of handling the request from client using Per-Call instance mode.
In per session only one instance of WCF service object is created for a session interaction. Below figure explains the same in a pictorial format. Client creates the proxy of WCF service and makes method calls. One WCF service instance is created which serves the method response. Client makes one more method call in the same session.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With