Monday 28 December 2015

Can you control when the service instance is recycled?


Yes, we can control when the service instance is recycled using the ReleaseInstanceMode property of the OperationBehavior attribute. You can control the lifespan of your WCF service. You can set the value of ReleaseInstanceMode property as one of the following:

  • RealeaseInstanceMode.None: No recycling behavior.
  • RealeaseInstanceMode.BeforeCall: Recycle a service object before an operation is called.
  • RealeaseInstanceMode.AfterCall: Recycle a service object after an operation is called.
  • RealeaseInstanceMode.BeforeAndAfterCall: Recycle a service object both before and after an operation is called.


In this post I explained how to control when the service instance is recycled. I hope it is very helpful for you. Please comments your feedback and quries. Thank You.

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.