Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MemoryCache and multiple per call WCF services

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:

  1. the services are all hosted in the same app in IIS
  2. the services are hosted in different IIS applications on the same server
like image 908
Liviu Mandras Avatar asked Jul 11 '14 13:07

Liviu Mandras


People also ask

What is the use of caching in WCF?

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.

What is a per call service in WCF?

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.

What is per-call instance mode in WCF?

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.

How many instance of WCF service is created per session?

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 Answers

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

like image 186
BRAHIM Kamel Avatar answered Oct 07 '22 01:10

BRAHIM Kamel