.netWCF

Instancing modes in WCF (Part-25)

Instance context mode identifies how long the service instance remains on server.

There are 3 instancing modes in WCF.

  • Percall
    • A new instance of service object is created for every request regardless if the request is coming from same client or different client
  • Persession
    • A new instance of service object is created for each new client session and maintained for the duration of that session
  • Single
    • A new instance of service object is created and handles all request for the lifetime of the application regardless if the request is coming from same client or different client

How to specify the Instancing mode?

You can use servicebehavior attribute and specify instanceContextMode to use it.

[service behavior (InstanceContextMode = InstanceContextMode.PerCall]

PerCall instance Context mode:

  • A new instance of wcf service is created for each request regardless of client
  • Application scalability is better
  • Better memory usage
  • State not maintained between calls
  • Performance could be an issue due to new instance for each service call
  • Concurrency is not an issue
  • If you prefer SOA(Service oriented architecture) then perCall instance can be used
  • [service behavior (InstanceContextMode = InstanceContextMode.PerCall]

PerSession instance context mode:

  • A new instance of wcf service is created for each new clients and maintained for duration of that session
  • State maintained between calls
  • More memory consumption since the service objects remain in memory till client session times out
  • Concurrency is an issue for multi threading client
  • If you want object oriented architecture,PerSession instance can be used
  • [service behavior (InstanceContextMode = InstanceContextMode.PerSession]

Single instance context mode:

  • when the instance context mode for a wcf service is set to Single then only one instance of wcf service is created to handle all request
  • [service behavior (InstanceContextMode = InstanceContextMode.Single]
  • State is maintained and shared between request from same client and also from different client
  • Concurrency is an issue
  • Throughput can be an issue

 

© 2015, www.techkatak.com. All rights reserved.