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");