tags:

views:

419

answers:

2

I am trying to discern the difference between SingleCall and Singleton activation methods when implementing a server to host an object using .NET Remoting. It would appear that SingleCall has the overhead of having to construct and clean up an object for each client-side call, whereas Singleton has the limitation of only being able to service a limited number of simultaneous requests. I am looking to make performance as good as possible. Which should I choose?

+1  A: 

By default, you should use SingleCall.

Also, keep in mind that, when using SingleCall objects, you cannot share state accross calls.

I found this site a good resource when it comes to .NET remoting: http://www.thinktecture.com/resourcearchive/net-remoting-faq/remotingusecases

Frederik Gheysels
+1  A: 

You are right. SingleCall builds the object per each call and can accept multiple simultaneous requests, but data can't be shared between calls, while Singleton builds a single object to handle multiple calls allowing data sharing, but limits simultaneous connections. However, there are tweaks that you can do if you have some concept of how to build thread safe objects.

First, I would suggest using the Singleton as it is created only once for many. This also has the advantage of allowing you to store information and share it between users connecting to it without having to constantly hit an outside store.

Second, I would look into adding the ConcurrencyMode=ConcurrencyMode.Multiple into the ServiceBehaviors of your service. This allows multiple users to hit your singleton simultaneously.

[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)] 
public class CalculatorService : ICalculatorConcurrency 
{ 
    …
}

Third, clean up any code that would make this class not thread safe. You should lock the object when accessing local variables that multiple threads could access simultaneously.

Lots of good information about these topics can be found here:

http://msdn.microsoft.com/en-us/library/ms731193.aspx

Lusid