Instance management defines the binding of service instance to a request received from a client.in otherwords, Instance context mode identifies how long the service instance remains on server.
There are 3 instancing modes in WCF.
- A new instance of service object is created for every request regardless if the request is coming from same client or different client
- A new instance of service object is created for each new client session and maintained for the duration of that session
- 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.
public interface IInstanceManagDemoService
public class InstanceManagDemoService : IInstanceManagDemoService
public string Method1()
//method details here…..
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]
Reference books, you may like:Programming WCF Services: Design and Build Maintainable Service-Oriented Systems
WCF Multi-Layer Services Development with Entity Framework, 4th Edition
Learning WCF: A Hands-on Guide
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, admin. All rights reserved.