Monday, 28 December 2015

How is the service instance created? How can you manage or control WCF service instance creation?


Service Instance

Client request can be served by using single service instance for all users, one service instance for one client, or one instance for one client request. You can control this behavior by using the technique called Instance Management in WCF.

There are three instance modes supported by WCF:


  • Per-Call: Service instance is created for each client request. This Service instance is disposed after response is sent back to client.
  • Per-Session (default): Service instance is created for each client. Same instance is used to serve all the requests from that client for a session. When a client creates a proxy to particular service, a service instance is created at server for that client only. When session starts, context is created and when it closes, context is terminated. This dedicated service instance will be used to serve all requests from that client for a session. This service instance is disposed when the session ends.
  • Singleton: All client requests are served by the same single instance. When the service is hosted, it creates a service instance. This service instance is disposed when host shuts down.



You can configure instance mode using [ServiceBehavior] attribute as below:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]

public class MyService:IGreetingService



In this post I explained what are the Instance mode of supported by WCF. I hope you enjoyed this post please comments your feed back and quires. Thank You.

No comments:

Post a Comment