0

i am very new in WCF. so often gaze for wcf code & article. after viewing many code i often stuck for very basic things and got no elaborate discussion for the below question. so here are my few basic question....and looking for details discussion with sample situation and with sample code.

what is ConcurrencyMode ?? actually what ConcurrencyMode does.....what it determines?? there are few ConcurrencyMode available and those are Multiple,Reentrant and Single

which concurrent mode one should use in what kind of situation. what will be the best choice. thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626

1 Answers1

0

It depends on what you need. If you need a single instance of your service to be instantiated for the life time of the application you might want to use single. You might do this for instance if instantiating that service was extremely expensive for some reason and it was not called very often so you weren't concerned about multiple threads hitting it at the same time.

    [ServiceBehavior(Namespace = "http://somenamespace", InstanceContextMode = InstanceContextMode.Single)]
public sealed class ServiceWithExpensiveDictionary : IServiceWithExpensiveDictionary, IDisposable
{
    private DataCacheFactory factory;
    private ConcurrentDictionary<string, DataCache> dataCacheDictionary;

    /// <summary>
    /// Constructor
    /// </summary>
    public ServiceWithExpensiveDictionary()
    {
        factory = new DataCacheFactory();

        dataCacheDictionary = new ConcurrentDictionary<string,DataCache>();
    }

In the code above, I want the ConcurrentDictionary instance to be around for all callers of the service as I'm putting expensive objects into it.

You might use session if you were intending users to have stateful long-running conversations with your api.

You might use per call if your services were stateless and service instantiation was cheap.

It really depends on what you want to do.

There is also a nice question related to this here. It also touches on throttling which you might also be interested in:-

WCF ConcurrencyMode Single and InstanceContextMode PerCall

Community
  • 1
  • 1
Nick Ryan
  • 2,662
  • 1
  • 17
  • 24
  • sorry really do not understand what u said...can u please use some sample code with various option just to make me understand. thanks – Thomas Jan 03 '13 at 12:08
  • @Thomas I put in an example for Single. In the rest of my app, it's all per call at the moment - as all the other services are stateless. – Nick Ryan Jan 03 '13 at 12:19