Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am going to build a Windows Communication Foundation (WCF) service using Microsoft Enterprise Library for caching. I am wondering whether or not I should put the cache in the service layer. If I do this, do I have to use InstanceContextMode = Single for this to work? Are there better alternatives, because I prefer using InstanceContextMode = PerSession. Where could I put the cache?

share|improve this question
up vote 1 down vote accepted

Perhaps not the ideal answer but static variables will still be persisted throughout the life of the application in either InstanceContextMode.

Singeltons are usually lauded for classes, but for large data structures, unless you use a distributed caching solution, there isn't a lot of choice. Besides, the very nature of a cache entails that there will only be 1 on a given server.

InstanceContextMode Single is far worse (in most scenarios--Only one request per service can process at a time; not desirable)

share|improve this answer
Thanks for answer. So if i use PerSession only private ,public and protected variables will live in the session and disposes after the session ends ? – Tan Feb 8 '12 at 8:42
It sounds like InstanceMode and Concurrency are being confused in this answer. If you use a Single context mode without making the ConcurrencyMode multiple you would run into the scenario outlined. See this CodeProject article for an explanation of each option: (…) – Kevin Jan 7 '14 at 13:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.