0

I have a WCF Rest web service using OutputCaching which is configured in the Web.config file. The caching appears to be working fine as it is configured now. I would like to be able to clear this OutputCache from a separate front-end application by posting a service call to the web service.

Current Web.config file. Note there are multiple Profiles that were removed for brevity:

<caching>
  <outputCache enableOutputCache="true" />
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="AgentsByAgentId" location="Server" duration="600" varyByParam="agentId;callback" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

Current interface setup:

[ServiceContract]
public interface IAgentSearch
{
    [AspNetCacheProfile("SearchByAgentId")]
    [WebGet(UriTemplate = "/agents?agentId={agentId}", ResponseFormat = WebMessageFormat.Json)]
    [Description("Search by agent ID.")]
    [OperationContract]
    IEnumerable<Agent> SearchByAgentId(string agentId);

    ... // Other methods removed for brevity.

    [WebInvoke(Method = "POST", UriTemplate = "/clearcacheprofiles")]
    [Description("Method to clear all output cache for the service.")]
    [OperationContract]
    void ClearCacheProfiles();
}

Any ideas on how I can accomplish this?

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class SmartSearchService : ISmartSearchService
{
    public IEnumerable<Agent> SearchByAgentId(string agentId)
    {
        ...
    }

    public void ClearCacheProfiles()
    {
        // What is the best way to clear all OutputCacheProfiles?
    }
}

I'm not opposed to caching in a different way if that would make it easier. Any advice is greatly appreciated! Thanks in advance!

Update:
I've tried the following in the ClearCacheProfiles() method without success.

System.Web.HttpResponse.RemoveOutputCacheItem("/agents");

and

System.Web.HttpResponse.RemoveOutputCacheItem("/agents?agentId=<some_id_i_searched>");

Update:
I simply messed up the virtual paths... Doh! I finally put together a quick, junk custom OutputCacheProvider in order to see the keys that were being passed to the Add, Get, Remove and Set methods of the provider.

This worked as expected *face palm*

System.Web.HttpResponse.RemoveOutputCacheItem("/<virtual_directory/<service_name>/agents");
Urk
  • 880
  • 8
  • 16
  • Does this point in the right direction? This is an MVC answer, but it might be transferable? https://stackoverflow.com/questions/1167890/how-to-programmatically-clear-outputcache-for-controller-action-method – Craig H May 16 '19 at 15:33
  • @CraigH Thanks for the suggestion! Sorry, I was mistaken and this is a WCF web service, not Web API. I've tried similar things like HttpResponse.RemoveOutputCacheItem("/agents") and tried more specific virtual paths like HttpResponse.RemoveOutputCacheItem("/agents?agentId="), but no luck so far. I notice most people are specifying their OutputCache settings via attributes over the Controller Actions, however I have neither Controllers nor Actions and OutputCache isn't a valid attribute on these methods. :/ – Urk May 16 '19 at 16:48
  • 1
    @CraigH Thanks again, I figured out that I was using the wrong value for the virtual path, and after correcting it, the HttpResponse.RemoveOutputCacheItem() method is working as expected. Thanks for the time! – Urk May 16 '19 at 17:51
  • 1
    Hey @Urk, glad this was of use! One thing to note, I think the default output cache is memory based. If you use a multi-server environment then this would only clear the cache on the server which received the clear request. Then you would need to look at some centralised cache perhaps, or a way to share the request to clear to all servers. – Craig H May 17 '19 at 08:06
  • @CraigH Ah thanks for that, these servers are indeed load balanced. Duly noted! Thanks again! – Urk May 17 '19 at 14:07

0 Answers0