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?


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)

  • 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: (codeproject.com/Articles/89858/…) – Kevin Jan 7 '14 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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