Caching in WCF Services
Suppose, user sends request to a service so at server, than service method is called and it response and sent result back to user. But, if we configure caching at service, then service method won’t called, cached response is sent back. If cache expired, then next time service method is called and the response will again cache.
How caching is implemented in WCF services?
1. Mandatory Configuration: In service Side 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>
<add name = ""
enabled = "true"
duration = "-1"
location = ""
sqlDependency = ""
varyByCustom = ""
varyByControl = ""
varyByHeader = ""
varyByParam = ""
noStore = "false"/>
</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.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
[AspNetCacheProfile("CustomCache")]
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
No comments:
Post a Comment