Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF ConcurrencyMode Single and InstanceContextMode PerCall

Tags:

I have an issue with my wcf service config. I would like every call to my service create a new instance of the service. For the concurrency I would like to one call is finished before another start.

Thus if I have a service like this one:

[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Single,
InstanceContextMode=InstanceContextMode.PerCall)]
public class MyService: IMyService
{
    public bool MyServiceOp()
    {
        Debug.WriteLine("thread "+ 
            Thread.CurrentThread.ManagedThreadId.ToString());
        Debug.WriteLine("start operation ");
        Do_work()
        Debug.WriteLine("end operation");
        return true;
    }
}

When I call it with multiple call in a loop, the trace give:

thread 1
thread 2
start operation
start operation
end operation
end operation

While I would like to have this:

thread 1 start operation end operation
thread 2 start operation end operation

Is this possible? Thank you

like image 902
maxence51 Avatar asked Aug 19 '11 13:08

maxence51


People also ask

What is InstanceContextMode in WCF?

InstanceContextMode property to specify the lifetime of the InstanceContext object. Windows Communication Foundation (WCF) can create a new InstanceContext object for every call, every session, or specify that the InstanceContext object is bound to a single service object. For a working example, see Instancing.

Which of the following are instancing modes available in WCF?

The following instancing modes are available: PerCall: A new service instance is created for each client request. PerSession: A new instance is created for each new client session, and maintained for the lifetime of that session (requires a binding that supports session).

In which instance mode complete session of a user is maintained?

WCF can maintain a session between a client and a particular service instance.


2 Answers

I know this question was marked as answered, but there is a better alternative:

If you use a InstanceContextMode.Single then you will reuse the same instance for all calls. If your service is long running this requires your code to manage resources perfectly, since it will never be garbage collected without a service restart.

Instead keep the InstanceContextMode.PerCall for “every call to my service creates a new instance” and then use throttling: Set the max concurrent instances to 1. The MSDN documentation does exactly this as one of the examples.

like image 92
ErnieL Avatar answered Sep 21 '22 09:09

ErnieL


What you have there will result in a new instance of the service spinning up with each request (that's what PerCall does).

This should do it:

[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Single, InstanceContextMode=InstanceContextMode.Single)]

FYI if you do this you'll lose all scalability. You'll have a single instance of a single threaded service to respond to all requests.

like image 20
Patrick Sears Avatar answered Sep 17 '22 09:09

Patrick Sears