Three ways to do WCF instance management
2012-10-30 14:42
239 查看
By Shivprasad koirala, 9
Jun 2010
http://www.codeproject.com/Articles/86007/3-ways-to-do-WCF-instance-management-Per-call-Per
Download source code - 39.1 KB
WCF
service object instancing basics
Per
call instance mode
How
to implement WCF per call instancing
Per
session instance mode
How
to implement per session instancing
Single
instance mode
How
to implement single instance mode
When
should you use per call, per session, and single mode?
Per call
Per session
Single
References
Source code
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.
There is a small ebook for all my .NET friends which covers topics like WCF, WPF, WWF, AJAX, Core .NET, SQL, etc., which you can download from here or
you can catch me on my daily free training from here.
WCF client makes a request to a WCF service object.
WCF service object is instantiated.
WCF service instance serves the request and sends the response to the WCF client.
Following is a pictorial representation of how WCF requests and responses work.
Following are different ways by which you can create WCF instances:
Create a new WCF service instance on every WCF client method call.
Only one WCF service instance should be created for every WCF client session.
Only one global WCF service instance should be created for all WCF clients.
To meet the above scenarios, WCF has provided three ways by which you can control WCF service instances:
Per call
Per session
Single instance
The WCF client makes the first method call (method call 1).
A new WCF service instance is created on the server for this method call.
The WCF service serves the request and sends the response and the WCF instance is destroyed and given to the garbage collector for clean up.
Now let’s say the WCF client makes a second method call, a new instance is created, the request is served, and the WCF instance is destroyed.
In other words, for every WCF client method call, a WCF service instance is created, and destroyed once the request is served.
as shown below. This attribute needs to specified on the
a class level variable and the class counter is incremented by one when the
Collapse | Copy
Code
At the client, we consume the WCF client and we call the
Collapse | Copy
Code
Even though we have called the
is created for every method call made to the WCF service instance so the value will always be one.
The figure below explains this in pictorial format.
The client creates the proxy of the WCF service and makes method calls.
A WCF service instance is created which serves the method response.
The client makes one more method call in the same session.
The same WCF service instance serves the method call.
When the client finishes its activity, the WCF instance is destroyed and served to the garbage collector for clean up.
in the
Collapse | Copy
Code
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.
Collapse | Copy
Code
mode. Below is a simple pictorial notation of how the single instance mode will operate:
WCF client 1 requests a method call on the WCF service.
A WCF service instance is created and the request is served. The WCF service instance is not destroyed, the service instance is persisted to server other requests.
Now let’s say some other WCF client, e.g., client 2, requests a method call.
The same WCF instance which was created by WCF client 1 is used to serve the request for WCF client 2. In other words, only one global WCF server service instance is created to serve all client requests.
Collapse | Copy
Code
If you call the WCF service from a different client, you will see the counter incrementing. The counter becomes a global variable.
Your service holds intensive resources like connection objects and huge memory objects.
Scalability is a prime requirement. You would like to have a scaled out architecture.
Your WCF functions are called in a single threaded model.
You a scaled up architecture.
Light resource references.
Scalability is not a concern.
Do not miss this post which covers end to end about WCF sessions:http://codeidol.com/csharp/wcf/Instance-Management/.
Great blog by Rick rain on WCF instancing: http://blogs.msdn.com/b/rickrain/archive/2009/06/15/wcf-instancing-concurrency-and-throttling-part-1.aspx.
(CPOL)
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training
and Interview questions and answers
Jun 2010
http://www.codeproject.com/Articles/86007/3-ways-to-do-WCF-instance-management-Per-call-Per
|
Table of contents
IntroductionWCF
service object instancing basics
Per
call instance mode
How
to implement WCF per call instancing
Per
session instance mode
How
to implement per session instancing
Single
instance mode
How
to implement single instance mode
When
should you use per call, per session, and single mode?
Per call
Per session
Single
References
Source code
Introduction
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.
There is a small ebook for all my .NET friends which covers topics like WCF, WPF, WWF, AJAX, Core .NET, SQL, etc., which you can download from here or
you can catch me on my daily free training from here.
WCF service object instancing basics
In normal WCF request and response communication, the following sequence of actions takes place:WCF client makes a request to a WCF service object.
WCF service object is instantiated.
WCF service instance serves the request and sends the response to the WCF client.
Following is a pictorial representation of how WCF requests and responses work.
Following are different ways by which you can create WCF instances:
Create a new WCF service instance on every WCF client method call.
Only one WCF service instance should be created for every WCF client session.
Only one global WCF service instance should be created for all WCF clients.
To meet the above scenarios, WCF has provided three ways by which you can control WCF service instances:
Per call
Per session
Single instance
Per call instance mode
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:The WCF client makes the first method call (method call 1).
A new WCF service instance is created on the server for this method call.
The WCF service serves the request and sends the response and the WCF instance is destroyed and given to the garbage collector for clean up.
Now let’s say the WCF client makes a second method call, a new instance is created, the request is served, and the WCF instance is destroyed.
In other words, for every WCF client method call, a WCF service instance is created, and destroyed once the request is served.
How to implement WCF per call instancing
In order to specify the instancing mode, we need to provide theInstanceContextModevalue in the
ServiceBehaviorattribute
as shown below. This attribute needs to specified on the
Serviceclass. In the below code snippet, we have specified
intCounteras
a class level variable and the class counter is incremented by one when the
Incrementmethod is called.
Collapse | Copy
Code
[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
Incrementmethod twice.
Collapse | Copy
Code
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient(); MessageBox.Show(obj.Increment().ToString()); MessageBox.Show(obj.Increment().ToString());
Even though we have called the
Incrementmethod 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.
Per session instance mode
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.
The client creates the proxy of the WCF service and makes method calls.
A WCF service instance is created which serves the method response.
The client makes one more method call in the same session.
The same WCF service instance serves the method call.
When the client finishes its activity, the WCF instance is destroyed and served to the garbage collector for clean up.
How to implement per session instancing
To configure service as per session, we need to configure theServiceBehaviorattribute with a
PerSessionvalue
in the
InstanceContextModeobject.
Collapse | Copy
Code
[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.
Collapse | Copy
Code
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient(); MessageBox.Show(obj.Increment().ToString()); MessageBox.Show(obj.Increment().ToString());
Single instance mode
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 asSingleinstance
mode. Below is a simple pictorial notation of how the single instance mode will operate:
WCF client 1 requests a method call on the WCF service.
A WCF service instance is created and the request is served. The WCF service instance is not destroyed, the service instance is persisted to server other requests.
Now let’s say some other WCF client, e.g., client 2, requests a method call.
The same WCF instance which was created by WCF client 1 is used to serve the request for WCF client 2. In other words, only one global WCF server service instance is created to serve all client requests.
How to implement single instance mode
In order to create a single instance of a WCF service, we need to specifyInstanceContextModeas
Single.
Collapse | Copy
Code
[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.
When should you use per call, per session, and single mode?
Per call
You want a stateless services.Your service holds intensive resources like connection objects and huge memory objects.
Scalability is a prime requirement. You would like to have a scaled out architecture.
Your WCF functions are called in a single threaded model.
Per session
You want to maintain states between WCF calls.You a scaled up architecture.
Light resource references.
Single
You want share global data through your WCF service.Scalability is not a concern.
References
MSDN link for WCF instances: http://msdn.microsoft.com/en-us/library/ms733040.aspx.Do not miss this post which covers end to end about WCF sessions:http://codeidol.com/csharp/wcf/Instance-Management/.
Great blog by Rick rain on WCF instancing: http://blogs.msdn.com/b/rickrain/archive/2009/06/15/wcf-instancing-concurrency-and-throttling-part-1.aspx.
Source code
You can download the source code for this tutorial from here.License
This article, along with any associated source code and files, is licensed under The Code Project Open License(CPOL)
About the Author
Shivprasad [b]koirala[/b] Architect http://www.questpond.com India Member |
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training
and Interview questions and answers
相关文章推荐
- Three ways to do WCF instance management
- Three ways to do WCF instance management
- Three ways to do WCF instance management
- 3 ways to do WCF instance management (Per call, Per session and Single)
- 3 ways to do WCF Concurrency Management(Single, Multiple, and Reentrant and How to do with Throttling)
- Three ways to use Perl to open a temporary file
- Some Articles on WCF Instance and Concurrency Management
- Three ways to implements an Android OnClickListener
- 4 ways to do concurrency in Java: Threads, Executors, ForkJoin and Actors
- 转一篇blog,这个人思路蛮清晰的 [do you want to work for a company with uninvolved management and a sweatshop mentality in which you occasionally see those people superior to you?]
- Three Ways to Insert CSS(浅析三种插入css的方法)
- Three Ways to Inject Your Code into Another Process
- Three Ways to Inject Your Code into Another Process
- Three ways to get your MAC address
- Note--three ways to host a service
- Step by Step WCF—Instance Management
- Three ways to set specific DeviceFamily XAML Views in UWP
- Three Ways To Inject Your Code Into Another Process
- Three Ways to Inject Your Code into Another Process
- Three ways to use C# within a VB.NET project