Caching in WCF Services
Oct08

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...

Read More
Caching is an evil
Nov05

Caching is an evil

I 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 still, live website was showing old content plus the developer forgot that caching is ON. Just imagine, development team is doing a night out to resolve this issue and after spending countless hours, people came to know that page is cached. Believe me, i have seen many situations like these. 😛 Next , when Client asks why it happened? They receive a very familiar reply “Caching Issue”. I got caught in this situation few times and my reply was the same as above. Client nodded and everyone forgot. Lately, client put a query that what is this caching? why it is a recurring issue? why can’t we disable it ? its more of a pain for us. Prior to this, client nodded whenever this issue came unaware of the issue so I must admit that this is a query asked for some personal knowledge. Let’s see how can we relate it to common terms. A person goes to a restaurant and orders something from la-carte and then waits for the order to arrive. Few minutes and food is on the table.yummy!!! Did anybody notice that there is some pickle,some salad, some sausages are already on the table? Nobody ordered them but they are already there on the table.Though they doesn’t form the complete meal but still without them food doesn’t taste well. So this already prepared item on the table can be imagined as cached items. As a user,nobody notices this item but it forms a necessary requirement for a smooth web experience. Similar to these items, few/all sections of the site are prepared once and can be served again and again. if caching is removed, then each section/item has to be freshly prepared and served which will take additional time and consume more web resources. I hope this example make some sense and only your opinion can prove that. Please ask any question or provide more creative idea to explain caching....

Read More