WCF Instance management is a set of techniques to decide whether create a new service instance on each client request or use existing service instance that handles client request.
Advantages of WCF Instance Management
- The biggest advantage of instance management is the use of WCF sessions. With the use of WCF session, a client can use a single service instance for his each request. When a client calls the service, WCF use the existing service instance which also maintains state in the instance.
- WCF can make the service instance as singleton. Singleton means a single service instance is handled all client requests. State of the service instances is shared between different clients. This type of service in useful in Logging services.
- WCF sessions, and singleton restricts the creation of services instances and use the existing services instances, it improves the performance of WCF service. Service does not require creating the service instances on each client requests.
- It improves the scalability of service as it handles more client requests.
WCF Service Instance Modes
WCF use three types of service instance management modes:
PerCall Instance Mode
In the per call instance mode, WCF runtime will create a new service instance for each client request. This mode does not differentiate between existing client and a new client. Each request gets its own service instance.
PerCall instance mode client request handling steps are following:
- The Client calls the service.
- WCF runtime creates a new service instance and process the request.
- WCF return the response to client and destroy the instance.
- Client again calls the service.
- WCF runtime again creates a new service instance and process the request.
- WCF return the response to client and destroy the second instance.
PerSession Instance Mode
In the per session instance mode, WCF runtime creates one dedicated service instance for each client. All requests from the same client handled by same dedicated service instance. Dedicated service instance also maintain state between requests.
When the client issues a first request, WCF runtime creates a new service instance and maintain his state in memory. When the client issue another request, WCF first checks if the request is from existing client, if yes then use existing service instance and if not then create a new service instance.
Single Instance Mode
In the single instance mode, WCF uses one and only one service instance for every client requests. This service instance also shared his state with every client.
When the first client of the service issues the first request, WCF creates a new service instance and maintain the instance in memory. All the subsequent requests from the same client and the other clients use the same service instance.