Search code examples
c#asp.netwcfcaching

Issue with caching wcf service - "AspNetCacheProfileAttribute can only be used with GET operations"


I have a WCF service which does work but really needs the responses to be cached. I've followed some guidance online but I receive the following error:

AspNetCacheProfileAttribute can only be used with GET operations.

I've checked this in Postman to be completely sure that my client side code is not making the request in the wrong way, same result there.

The interface looks like this:

public interface INavbar
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]        
    List<Navbar.simpleNav> getNavList();
}

The service returns a List as a JSON object, the 2 changes I made to try and add the caching were to add AspNetCacheProfile attribute to the class and add the caching profile details to my web.config.

Attribute for class:

[AspNetCacheProfile("CacheFor180Seconds")]

web.config:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="CacheFor180Seconds" duration="180" varyByParam="none"/>
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

As far as I can tell it's all pretty standard stuff however it may be that I'm trying to do this the wrong way so any help is appreciated.

Many thanks


Solution

  • You need to use WebGet rather than WebInvoke

    The difference is subtle but important:

    WebInvoke

    Represents an attribute indicating that a service operation is logically an invoke operation and that it can be called by the WCF REST programming model.

    WebGet

    Represents an attribute indicating that a service operation is logically a retrieval operation and that it can be called by the WCF REST programming model.

    And further down, in the remarks section for WebInvoke:

    The WebInvokeAttribute attribute is applied to a service operation … and associates the operation with a UriTemplate as well as an underlying transport verb that represents an invocation (for example, HTTP POST, PUT, or DELETE). … The WebInvokeAttribute determines what HTTP method that a service operation responds to. By default, all methods that have the WebInvokeAttribute applied respond to POST requests. The Method property allows you to specify a different HTTP method. If you want a service operation to respond to GET, use the WebGetAttribute instead.