I'm reading several articles that state the default instancing is PerSession and others that say it is PerCall. I've been looking at examples of PerSession that contain this attribute on the interface for both the proxy and host.

Code Snippet

[ServiceContract(SessionMode = SessionMode.Required)]

in the host class I have a method

Code Snippet

public void MyMethod()



string sessionID = OperationContext.Current.SessionId;

Trace.WriteLine("Service session ID: " + sessionID);

MessageBox.Show("Counter = " + m_Counter,"MyService.MyMethod()");


and in the the client when I call the service I have this

Code Snippet

void OnCall(object sender,EventArgs e)


MyContractClient proxy = new MyContractClient();


IContextChannel channel = proxy.InnerChannel;

string sessionID = channel.SessionId;

Trace.WriteLine("Client session ID: " + sessionID);




I can comment out the SessionMode in both the proxy and the host like this

Code Snippet

[ServiceContract] //(SessionMode = SessionMode.Required)]

and the results are the same. The Service session ID and Client Session ID are the same as if the attribute doesn't affect the outcome.

Is the default instance context PerSession

Do I even really need to use the SessionMode = SessionMode.Required if the default is PerSession

Do I need to add this attribute to the Host service class like this:

Code Snippet

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

Re: Windows Communication Foundation (Indigo) Instance Context?


The default is PerSession.

PerCall: A new InstanceContext object is created, and recycled, succeeding each call.

PerSession: A new InstanceContext object is created per session, and the instance context is not sharable between multiple sessions.

Single: A single InstanceContext object is created and used for all incoming calls and is not recycled succeeding the calls.

Since the default is per session, then you don't need to include it if that use meets your requirements.

Hope this helps...


Re: Windows Communication Foundation (Indigo) Instance Context?


So, Scott

When i use PerCall, i can have different instance context objects during the same session

Let's say the client opens the connection, and then call's 10 operations on my service.

My service has the InstanceContext attribute set on PerCall, so for each operation an new instance context is created.

But each of those calls still come in the same session