1

I have a service that always returns the same results for a given parameter. So naturally I would like to cache those results on the client.

Is there a way to introduce caching and other effect inside the WCF pipeline? Perhaps a custom binding class that could site between the client and the actual HTTP binding.

EDIT:

Just to be clear, I'm not talking about HTTP caching. The endpoint may not necessarily be HTTP and I am looking at far more effects than just caching. For example, one effect I need is to prevent multiple calls with the same parameters.

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447

1 Answers1

0

The WCF service can use Cache-Control directives in the HTTP header to say the client how it should use the client side cache. There are many options, which are the part of HTTP protocol. So you can for example define how long the client can just get the data from the local cache instead of making requests to the server. All clients implemented HTTP, like all web browsers, will follow the instructions. If your client use ajax requests to the WCF server, then the corresponding ajax call just return the data from the local cache.

Moreover one can implement many interesting caching scenarios. For example if one set "Cache-Control" to "max-age=0" (see here an example), then the client will always make revalidation of the cache by the server. Typically the server send so named "ETag" in the header together with the data. The "ETag" represent the MD5 hash or any other free information which will be changed if the data are changed. The client send automatically the "ETag", received previously from the server, together inside the header of the GET request to the server. The server can answer with the special response HTTP/1.1 304 Not Modified (instead of the typical HTTP/1.1 200 OK response) and with the body having no data. In the case the client will safe to get the data from the local cache.

I use "Cache-Control:max-age=0" additionally with Cache-Control: private which switch off caching the data on the proxy and declare that the data could be cached, but not shared with another users.

If you want read more about caching control with respect of HTTP headers I'll recommend you to read the following Caching Tutorial.

UPDATED: If you want implement some general purpouse caching you can use Microsoft Enterprise Library which contains Caching Application Block. The Microsoft Enterprise Library are published on the CodePlex with the source code. As an alternative in .NET 4.0 you can use System.Runtime.Caching. It can be used not only in ASP.NET (see here)

I continue recommend you to use HTTP binding with HTTP caching if it only possible in your environment. In the way you could save many time of development and receive at the end more simple, scalable and effective application. Because HTTP is so important, one implemened already so much useful things which you can use out-of-the-box. Caching is oly one from the features.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @Oleg: Good point. If the service is RESTful and operations use HTTP GET then this is way to go. – Ladislav Mrnka Feb 26 '11 at 22:17
  • That is quite interesting, but the endpoint won't always be HTTP based and I want to do a heck of a lot more than just simple caching. – Jonathan Allen Feb 27 '11 at 01:34
  • @Jonathan Allen: I updated my answer with the reference to Microsoft Enterprise Library having general purpouse [Caching Application Block](http://msdn.microsoft.com/en-us/library/ff664753(v=PandP.50).aspx) or use [System.Runtime.Caching](http://msdn.microsoft.com/en-us/library/system.runtime.caching(VS.100).aspx). – Oleg Feb 27 '11 at 09:55
  • The question is not "how do I cache". The question is "how do I inject behaviors into the WCF pipeline". – Jonathan Allen Mar 01 '11 at 05:40
  • @Jonathan Allen: It seems to me that you change your question one more time. You should decide what you want **before** you ask the question. You can find enough information how to extend WCF with custom behaviors, see [here](http://msdn.microsoft.com/en-us/magazine/cc163302.aspx) for example, but before all you should be clear **what** should be the behavior, but the WCF pipeline sees much less as you see in the application level, so caching can't work so effective. – Oleg Mar 01 '11 at 06:46
  • The actual question has always been "Is there a way to introduce caching **and other effect** inside the WCF pipeline?" – Jonathan Allen Mar 02 '11 at 19:45