Very often we would like to control the way WCF service objects are instantiated on a WCF server. You would want to control how long the WCF instances should be residing on the server.
The WCF framework has provided three ways by which we can control WCF instance creation. In this article, we will first try to understand those three ways of WCF service instance control with simple code samples of how to achieve them. Finally, we will compare when to use them and under what situations.
In normal WCF request and response communication, the following sequence of actions takes place:
Following is a pictorial representation of how WCF requests and responses work.
Following are different ways by which you can create WCF instances:
To meet the above scenarios, WCF has provided three ways by which you can control WCF service instances:
When we configure a WCF service as per call, new service instances are created for every method call you make via a WCF proxy client. The image below shows this in a pictorial format:
In other words, for every WCF client method call, a WCF service instance is created, and destroyed once the request is served.
In order to specify the instancing mode, we need to provide the InstanceContextMode
value in the ServiceBehavior
attribute as shown below. This attribute needs to specified on the Service
class. In the below code snippet, we have specified intCounter
as a class level variable and the class counter is incremented by one when the Increment
method is called.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Percall)] public class Service : IService { private int intCounter; public int Increment() { intCounter++ return intCounter; } }
At the client, we consume the WCF client and we call the Increment
method twice.
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient(); MessageBox.Show(obj.Increment().ToString()); MessageBox.Show(obj.Increment().ToString());
Even though we have called the Increment
method twice, we get the value ‘1’. In other words, the WCF service instance is created for every method call made to the WCF service instance so the value will always be one.
Very often we need to maintain state between method calls or for a particular session. For those kinds of scenarios, we will need to configure the service per session. In per session, only one instance of a WCF service object is created for a session interaction. The figure below explains this in pictorial format.
To configure service as per session, we need to configure the ServiceBehavior
attribute with a PerSession
value in the InstanceContextMode
object.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class Service : IService { private int intCounter; public int Increment() { intCounter++ return intCounter; } }
At the client side, when we run the below client code, you should see the value ‘2’ after the final client code is executed. We have called the method twice so the value will be seen as two.
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient(); MessageBox.Show(obj.Increment().ToString()); MessageBox.Show(obj.Increment().ToString());
Often we would like to create one global WCF instance for all WCF clients. To create a single instance of a WCF service, we need to configure the WCF service as Single
instance mode. Below is a simple pictorial notation of how the single instance mode will operate:
In order to create a single instance of a WCF service, we need to specify InstanceContextMode
as Single
.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class Service : IService { }
If you call the WCF service from a different client, you will see the counter incrementing. The counter becomes a global variable.
You can download the source code for this tutorial from here.
This is a very clearn and logical article posted on CodeProject By 8 Jun 2010.As it's very easy to understand ,so I'm not plan to translate it into Chinese.In order to Show Respect to the author,I decide to give the link to the orginal article below-click here.
,You can refer to some of WCF:
other articles written on