Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreaded Singleton WCF Service

Tags:

wcf

In his book "Programming WCF Services", Juval Lowry expresses concerns about using Singleton Services because of the performance implications.

In one of my projects I'm using a stateless singleton WCF Service declared like this:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
   ConcurrencyMode = ConcurrencyMode.Multiple)]
public class FooService : IFoo
{
}

The service is accessed from multiple Silverlight clients over httpsTransport. I've opted for a singleton because I see no need to burden the system the GC overhead when it is not really needed. Am I missing something or shouldn't this be the most efficient way to implement a stateless service that's equally fast if not faster than a PerCall instantiated service?

like image 771
Oliver Weichhold Avatar asked Jun 15 '11 13:06

Oliver Weichhold


1 Answers

Your assumption probably holds true for a WCF service configured for basicHttpBinding with no SSL (see here for more info) but that is not likely true for other bindings. Although your application code may truly be stateless and/or thread safe, WCF uses sessions internally to support functionally in other bindings. This implies only one session per request can be handled since there is only a single service instance.

Seems like picking the singleton pattern is a case of premature optimization. Optimizing for GC efficiency should only be a concern when you have a proven need.

like image 152
Sixto Saez Avatar answered Sep 24 '22 01:09

Sixto Saez