WCF was designed with a lot of options for you to tweak so your services run the way you need them to.  This flexibility is probably my favorite feature of WCF because you have a lot of power to choose things like instancing mode, transactional support, and so on.  The drawback is "discovering" how these things work together.

I have a transactional queued service that needs to behave like a singleton.  This service holds onto a resource that must always be available and there should not be many instances of it running (it’s a TCP socket listener so I don’t want a new one for every message I process).  During testing I discovered that though I had the InstanceContextMode set to Single WCF was still creating a new instance for each message received.  After some research I decided to tweak a few knobs starting with ReleaseServiceInstanceOnTransactionComplete.

While that name is probably longer than what Mcconnell might suggest it sure does make it clear what behavior to expect.  The property is a boolean but I went ahead and set it specifically to false and voilà, problem solved.  My service now behaves as a singleton.  For good measure I also set the ConcurrencyMode to Multiple to ensure that my service can be accessed by multiple threads.  Because I’m doing a lot of asynchronous work it will perform better under these settings.

Technorati Tags: ,,,