.NET Framework - Caching data for a WCF service

Asked By jeff on 12-Feb-08 06:04 PM
Hi there

Very new to C# so obviously thought I'd jump in the deep end first.

I want to create a WCF service that provides data from a database to a
large number of clients.

Many clients will try to connect in a very short space of time to
retrieve the same date.

To reduce the round trips to the database I want to cache the data in a

Simply put I have a class that provides access to the data in the
dataset but I don't want a new instance of this class and hence a new
data set created every time a client connects.

What I want is for all clients to have access to read from the same set
of data and hence from the same instance of the dataset.

The service is hosted as a windows service, not through IIS.

I am assuming a new instance of the class that implements the service
contract is created with each new client? Is this correct?

Any ideas folks - can provide more info if required.

pbromber replied on 12-Feb-08 08:02 PM
You can use the ASP.NET Cache class by specifying  HttpRuntime.Cache, as long
as you have a reference to System.Web. There does not actually have to be a
current HttpContext to use it.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
jeff replied on 13-Feb-08 02:44 AM
Thanks Peter I'd looked at that particular bestie for something else so
no problems.

I've followed the code from the help files in VS 2K8 to understand this
thing. However there is a bit of a void in my understanding between the
declaration of the service host in the windows service class and the
functionality implemented by the WCF service itself.

Is there a good guide you can point me in that describes what happens at
run time and what the interaction is between serviceHost and
MyWCFService implementation?
Nicholas Paldino [.NET/C# MVP] replied on 13-Feb-08 03:20 PM

A lot of this is going to depend on the instancing for the service that
you are specifying.

For example, it can be set to be a singleton (one instance for all
calls), sessionful (one instance per proxy), or per-call (one instance per
call on a proxy).  These will determine how your instances are created.

That's the first thing you are going to have to figure out in order to
figure out how to cache your data, as the cache is going to have a lifetime
that is relative (and slightly dependent) on this issue.

Also, you don't ^have^ to use the HttpCache from System.Web.  You can
also use the Enterprise Library Caching block, or you can just store the
data in a static variable somewhere (just make sure you synchronize access
to it, as depending on the instancing of your service and the scope of the
cache, you could have multiple threads accessing it at the same time).

- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com