WCF Instance Context Mode and Bindings

By Anoop Madhusudanan

Vote on HN

As well known, the instance context mode property of Service Behavior can have these three values.
  • PerCall - New instance is created for each client request.
  • PerSession -  New instance is created for each new client session, and will be available throughout the session
  • Single - Only one instance handles for all client requests throughout the application 
Example:

[ServiceBehavior(
     InstanceContextMode = InstanceContextMode.PerSession)]
 public class Service1 : IService1
    { 
       ...
    }

Now, what is the default value for the InstanceContextMode? I wrote a quick WCF app, and drilled down in debug mode.













As you might see, if you examine OperationContext.Current.InstanceContext and examine the Behaviour, you'll find useSession set to true, which means, it is PerSession.

But wait, let me change the binding to basicHttpBinding from wsHttpBinding - and let us have a look again.












Now you'll find that useSession is false, which means, the context mode is defaulting to PerCall, because basicHttpBinding can't support sessions. So the inference is, for bindings that support session, the default is PerSessions, other wise it is perCall.

Now, change the context mode in your service attribute to Single, and see what happens to the singleton variable in Behavior, in the above watch.

No comments:

Post a Comment

Please keep your comments clean.

© 2012. All Rights Reserved. Amazedsaint.com