Implementing a cache in a WCF service

I have a WCF service that caches certain data and uses it to respond to web requests. To handle this requirement, I made a Singleton service (using InstanceContextMode.Singleand ConcurrencyMode.Multiple(yes, this is threadafe)).

I tried to set the maximum service timeout using the following binding:

 <binding name="WebHttpBinding" receiveTimeout="24.20:31:23.6470000">
    <security mode="TransportCredentialOnly">
      <transport clientCredentialType="None" />
    </security>
 </binding>

My problem is that the service instance is dying at unpredictable intervals, which means that the first request of the web request will cause the cache to be restored (a very slow process).

Ideally, the cache will be rebuilt at a given time every day without the need for a web request. I can configure the application pool to be reused at the set time, but this still will not solve the problem that the service will not receive an instance before the first web request. I would prefer not to make a small scheduled script that sends a request to the service, as this is a kind of hacking.

Is there a better strategy for doing caching in a WCF service? What have others done? Is there any best practice?

+4
source share
5 answers

There is an MSDN article in Caching WCF Web HTTP Services Support , cited below:

.NET Framework 4 , ASP.NET -HTTP- WCF. WCF Web HTTP. HTTP GET , , ASP.NET , . , , HTTP GET, , ..........

:

+4

webservice.

, , , .

, -, global.asax -.

, "

[OperationContract]
public void GetLargeComplexData();

public GetLargeComplexData()
{
   // deserialize last cached data from db or file
   ...

   // Verify the deserialized cache is not invalid
   ...

   // if cache is invalid rebuild
   ...

   //return cached data
   ...
}
+1

receiveTimeout , . AppFabric, . , , , AppFabric . , , , .

0

An option you can use is to move the cache from the WCF service to a dedicated cache service such as Memcached or use Microsoft AppFabric Caching

This allows you to separate the cached data warehouse from the WCF service so you have more freedom in your data management and access architecture.

0
source

All Articles