Thomas
Thomas

Reputation: 34208

Regarding WCF InstanceContextMode

i am very new in WCF. so often gaze for wcf code & article. after viewing many code i often stuck for very basic things and got no elaborate discussion for the below question. so here are my few basic question....and looking for details discussion with sample situation and with sample code.

what is InstanceContextMode? many people use InstanceContextMode Single or PerCall or PerSession ?

i just need deep insight when i should go for InstanceContextMode Single or or PerCall or PerSession ? what it does basically ??

tell me briefly when i woul turn on Single what will happen or what will happen in case of PerCall or PerSession ? what is difference between Single or PerCall or PerSession

what is default InstanceContextMode ?

Upvotes: 0

Views: 345

Answers (1)

Brent M. Spell
Brent M. Spell

Reputation: 2267

The InstanceContextMode property indicates how WCF will create instances of your service class and whether those instances will be reused across requests.

  • PerSession: (the default) an instance of your service will be created for each WCF session, for channels that support sessions (otherwise, the behavior is the same as PerCall); this value is useful if you maintain state within your service class for each client session
  • PerCall: a new instance of your service class will be created for each WCF operation (method call) from the client
  • Single: only a single service instance will be created within the host process, which will service all incoming requests; all calls will be serialized to the service unless the service's ConcurrencyMode behavior is set to Multiple.

For high-volume stateless services, using Single and ConcurrencyMode.Multiple can reduce the number of service instances allocated and the associated pressure on the garbage collector. WCF doesn't require/assume that your service is stateless, though, which is why PerSession is the default.

You can verify the behavior of the different instance context modes by setting a breakpoint in your service's default constructor.

Upvotes: 2

Related Questions