none
AspNetCacheProfile feature for caching is not working in WCF Restful Service RRS feed

  • Question

  • Hi,

    We have a requirement to develop a restful WCF service in .Net Framework 4.0. We found that the WCF WebHTTP services would be the best option to go forward.

    We created a restful service based on this and the service has some functionality to retrieve data from the database. Since the data is static across all the requests, but varies depend on the parameter values. Here comes the caching of data whenever any new request is made so that next request onwards, the service can return the data from the cache.

    In order to achieve this we found that the .NET Framework 4 introduces a new attribute called AspNetCacheProfileAttribute which can be used to cache the data. And this can be achieved by enabling ASP.NET compatibility by applying the AspNetCompatabilityRequirementsAttribute to the service setting RequirementsMode to Allowed or Required.

    Sample Service Interface looks like

    public interface IMyService

    {

           [OperationContract]

           [AspNetCacheProfile("CacheData")]

           [WebGet(UriTemplate = "MYDATA/{value}")]

           String GetData(string value);

           

    }

    Sample Service class looks like

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public
    class MyService : IMyService

    {

    public String GetData(string value)

            {

                return value + DateTime.Now.ToString();

            }

    }

    Web.config

    <system.serviceModel>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

                       <services>

                                 <service behaviorConfiguration="MyServiceBehavior" name="MyServiceTest.MyService">

                                          <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" bindingConfiguration="Httpconfig" name="Custom" contract=" MyServiceTest.IMyService ">

                                          </endpoint>

                                 </service>

                       </services>

                       <behaviors>

                                 <serviceBehaviors>

                                          <behavior name=" MyServiceBehavior ">

                                                    <serviceMetadata httpGetEnabled="false"/>

                                                    <serviceDebug includeExceptionDetailInFaults="true"/>

                                          </behavior>

                                 </serviceBehaviors>

                                 <endpointBehaviors>

                                          <behavior name="webBehavior">

                                                    <webHttp/>

                                          </behavior>

                                 </endpointBehaviors>

                       </behaviors>

                       <bindings>

                                 <webHttpBinding>

                                          <binding name="Httpconfig">

                                                    <security mode="None">

                                                              <transport clientCredentialType="None"/>

                                                    </security>

                                          </binding>

                                 </webHttpBinding>

                       </bindings>

              </system.serviceModel>

    <system.web>

            <caching>

                    <outputCacheSettings>

                            <outputCacheProfiles>

                                    <add name="CacheData" duration="3600" varyByParam="*"/>

                            </outputCacheProfiles>

                    </outputCacheSettings>

            </caching>

    </system.web>

    The caching is working fine when we ran the service in ASP.Net Development Server built within VS2010. However when we hosted the same service in Development & Test Environments (Win2K8 Server & IIS 7.5) the caching doesn’t work.

    We tried with various settings in varyByParam and enabling Output Caching in IIS as well, but all with no hope.

    Also the service is called from a thick client application using HttpWebRequest using the URI.

    We are now totally clueless on how to resolve this. Can you please help?

    Thanks in advance.

    Sreeju


    Thursday, July 4, 2013 1:13 AM

Answers

All replies