When a WCF REST service has caching enabled, the underlying code (of course) is not run on subsequent calls to the same URI. However, I’m wondering if there is a way we could hook into the caching provider to see the request coming in and then log it. This would be for analysis purposes or to track API usage.
Share
WCF REST caching is done by ASP.NET caching module which is registered to handle
HttpApplication.ResolveRequestCacheandHttpApplication.UpdateRequestCacheevent. You just need to handle event beforeResolveRequestCache(most suitable would beBeginRequest) and access request directly inHttpContext.Current.Request(you can useInputStreamproperty to read pure HTTP request). These events are typically handled in Global.asax file.Be aware that caching can happen on multiple levels – client can have data in its own cache, proxy server can cache data, etc. so not necessarily all request which use cached response will hit your server. You can control where the data can be cached by setting
Locationin cache profile.