Caching in WCF Services

WCF
WCF @Image courtesy of ddpavumba/ FreeDigitalPhotos.net

Caching in WCF Services

Welcome to CodeSpread!

Yes, It is possible and clearly a  significant performance enhancement.

How caching works for WCF services?

Lets assume, a user sends a request to a service so at server, service method is called and response is sent back to the user.

Now, when we configure caching at service, then the service method is not called, instead a cached response is sent back.

If the cache expires, the next time service method is called and the response is again cached.

How caching is implemented in WCF services?

1) Mandatory Configuration: Enable ASP.NET compatibility mode in the Web.config file

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

 2) Attach a Cache Profile: Cache Profile settings are maintained in web.config under system.web settings

<outputCacheSettings>
<outputCacheProfiles>
CustomCache" duration="120" varyByParam="none" />
</outputCacheProfiles>
</outputCacheSettings>
  • Attributes of the cache profile are: Duration and varyByParam
  • Duration: Time duration for which the response is cached.
  • varyByParam: Separate cached responses for different query parameters.

3)  AspNetCacheProfile attribute

First, Enable AspNetCompatibilityRequirementsAttribute and set RequirementsMode to Allowed or Required. The default value is NotAllowed.

This attribute is applied to a service so that the service can run in ASP.NET compatibility code.

Next, Apply AspNetCacheProfileAttribute to a service operation. This attribute specifies a cache profile name. In our case, Cache profile name is “CustomCache” which we declared in web.config earlier.

[ServiceContract]

[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
public class CalculatorService
{
[WebGet(UriTemplate = "?num={num}")]
[AspNetCacheProfile("CustomCache")]
public int Calculate(int num)
{
// Do Calculation.
}
}

More:

There is more to caching like SQL Cache Dependency. WCF service may depend on data stored in a SQL database so when data in the SQL database table changes, it may require to recreate the cache.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • Caching is an evilCaching is an evilI know 'Caching' was created to ease out the network resources but i have seen the situations where it emerged as the biggest evil. I can remember many situations when the website is updated but...
  • WCF Part 1: Why WCF?WCF Part 1: Why WCF?When i create a WCF service, i feel like it is a very easy task but when somebody asks me, why you have created it? then the simplest answer is, i got the requirement like this. But to be honest, N...
  • REST- REpresentational State TransferREST- REpresentational State TransferREST – REpresentational State Transfer, is an enhanced version of client server style architecture for distributed communication. When we talk about distributed communication, we think of reques...
  • Few lines about WCF metadataFew lines about WCF metadataWhat is WCF metadata? WCF generates metadata for service endpoints and this metadata is used to describe how to interact with the service's endpoints. This information is used by Svcutil.exe to ge...
  • WCF Part 2: Fundamentals.WCF Part 2: Fundamentals.We have seen the evolution of WCF in our previous article: WCF Part 1: Why WCF?. Next, we will go through the fundamentals of WCF. I will try to make it more worthy than an interview question puff....
  • WCF Part 3: Direct from WCF kitchen.WCF Part 3: Direct from WCF kitchen.In the last two articles WCF:Part 1 and WCF Part 2, we have covered basic knowledge and terminologies of WCF.Now we will apply that knowledge to create a WCF service.There are lot of articles avail...
  • Difference Between Transport and Message Security in WCFDifference Between Transport and Message Security in WCFTransport Security in WCF As the name suggests, it is concerned with security of communication between client and the service over a network protocol. It guarantees the confidentiality and integri...
  • Error-Resolved : Too Many Redirects.Error-Resolved : Too Many Redirects.During Deployment, We faced one error "There are too many redirects happening......"  while navigating to login page. We first felt that this is an issue related to certificate header and can be re...