1
votes

I have ConcurrencyMode.Multiple ans InstanceContextmode.PerSession, but I do not understand the latter.

In my application I do like this:

ServiceHost host = new ServiceHost(typeof(MyService), baseAddress);

But http://msdn.microsoft.com/en-us/library/system.servicemodel.instancecontextmode(v=vs.110).aspx

tells that "For singleton lifetime behavior (for example, if the host application calls the ServiceHost constructor and passes an object to use as the service), the service class must set InstanceContextMode to InstanceContextMode.Single, or an exception is thrown when the service host is opened."

Isn't that what I'm doing? It works fine and it's multithreaded. I would really appreciate if someone could explain me PerSession and PerCall values. Isn't a session also a call?

1

1 Answers

1
votes

When you pass a service class type as parameter, you expect the instance of that class to be created on demand (when a client call is received). So you do not control the lifetime of the service instance yourself. There are 2 types of instancing:

PerCall: A new InstanceContext (and therefore service object) is created for each client request.

PerSession: A new InstanceContext (and therefore service object) is created for each new client session and maintained for the lifetime of that session (this requires a binding that supports sessions).

If you want to control the instancing yourself, you have to initialize the class and pass the object as the parameter to the ServiceHost constructor. That is called "single" instancing mode:

Single: A single InstanceContext (and therefore service object) handles all client requests for the lifetime of the application.

In that case you have to set the ServiceBehaviorAttribute.InstanceContextMode property to Single in your service class:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MyService : IMyService
{
    public void SomeMethodHere(parm) {}
}