简体   繁体   中英

Default InstanceContextMode in WCF

In WCF, if we don't specify the InstanceContextMode of the ServiceBehaviour specifically, what is the default mode of the following?

  1. PerCall
  2. PerSession
  3. Single

Thanks.

The documentation on it talks about default value is PerSession . But I believe it also depends upon the binding used (does binding support sessions), like basichttpbinding uses PerCall

The simple answer is that the default Instancing mode is PerSession

Provided:

Here is a Microsoft provided sample with the default imperatively configured in the code. Default behavior sample

[ServiceBehavior(  
AutomaticSessionShutdown=true,  
ConcurrencyMode=ConcurrencyMode.Single,  
InstanceContextMode=InstanceContextMode.PerSession,  
IncludeExceptionDetailInFaults=false,  
UseSynchronizationContext=true,  
ValidateMustUnderstand=true)]
public class CalculatorService : ICalculator { ... }

I found reading about session in this Microsoft article ( Using Sessions ) particularly enlightening in understanding how Sessions is opened and closed and how this relates to Instancing and Concurrency.

By default the WCF client will create a new session, which will create a server instance, all calls for the duration of the session is called a conversation and is served by a single instance (Instancing) of the server with a single thread (Concurrency) dedicated to that session/client/conversation.

If you use the default instancing behavior in WCF, all calls between a WCF client object are handled by the same service instance. Therefore, at the application level, you can think of a session as enabling application behavior similar to local call behavior. For example, when you create a local object:

A constructor is called.

All subsequent calls made to the WCF client object reference are processed by the same object instance.

A destructor is called when the object reference is destroyed.

Sessions enable a similar behavior between clients and services as long as the default service instance behavior is used.

Hope this helps someone as it took me a while to find the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM