Table of contents
- Introduction
- WCFService object instancing Basics
- Per call instance mode
- How to ImplementWCFPer 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 wowould like to control the wayWCFService objects are instantiated onWCFServer. You wocould want to control how longWCFInstances shocould be residing on the server.
TheWCFFramework has provided three ways by which we can controlWCFInstance creation. In this article, we will first try to understand those three waysWCFService instance control with simple code samples of how to achieve them. Finally, we will compare when to use them and under what situations.
WCF Service object instancing Basics
In normalWCFRequest and response communication, the following sequence of actions takes place:
- WCFClient makes a request toWCFService object.
- WCFService object is instantiated.
- WCFService instance serves the request and sends the response toWCFClient.
Following is a pictorial representation of howWCFRequests and responses work.
Following are different ways by which you can createWCFInstances:
- Create a newWCFService instance on everyWCFClient method call.
- Only oneWCFService instance shoshould be created for everyWCFClient Session.
- Only one globalWCFService instance shoshould be created for allWCFClients.
To meet the above scenarios,WCFHas provided three ways by which you can controlWCFService instances:
- Per call
- Per session
- Single Instance
Per call instance mode
When we configureWCFService as per call, new service instances are created for every method call you make viaWCFProxy client. The image below shows this in a pictorial format:
- TheWCFClient makes the first method call (method call 1 ).
- A newWCFService instance is created on the server for this method call.
- TheWCFService serves the request and sends the response andWCFInstance is destroyed and given to the Garbage Collector for clean up.
- Now let's sayWCFClient makes a second method call, a new instance is created, the request is served, andWCFInstance is destroyed.
In other words, for everyWCFClient method call,WCFService 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 provideInstancecontextmodeValue inServicebehaviorAttribute as shown below. This attribute needs to specified onServiceClass. In the below code snippet, we have specifiedIntcounterAs a class level variable and the class counter is incremented by one whenIncrementMethod is called.
[Servicebehavior (instancecontextmode =Instancecontextmode. percall)]Public ClassService: iservice {Private IntIntcounter;Public IntIncrement () {intcounter++ReturnIntcounter ;}}
At the client, we consumeWCFClient and we callIncrementMethod twice.
Servicereference1.serviceclient OBJ =NewServicereference1.serviceclient (); MessageBox. Show (obj. increment (). tostring (); MessageBox. Show (obj. increment (). tostring ());
Even though we have calledIncrementMethod twice, we get the value '1'. In other words,WCFService instance is created for every method call made toWCFService instance so the value will always be one.
Per session instance mode
very often we need to maintain state between method CILS or for a special 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 ofWCFService and makes method CILS.
- AWCFService instance is created which serves the method response.
- The client makes one more method call in the same session.
- The sameWCFService instance serves the method call.
- When the client finishes its activity,WCFInstance 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 configureServicebehaviorAttribute withPersessionValue inInstancecontextmodeObject.
[Servicebehavior (instancecontextmode =Instancecontextmode. persession)]Public ClassService: iservice {Private IntIntcounter;Public IntIncrement () {intcounter++ReturnIntcounter ;}}
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 =NewServicereference1.serviceclient (); MessageBox. Show (obj. increment (). tostring (); MessageBox. Show (obj. increment (). tostring ());
Single Instance Mode
Often we wowould like to create one globalWCFInstance for allWCFClients. To create a single instance ofWCFService, we need to configureWCFServiceSingleInstance mode. Below is a simple pictorial notation of how the single instance mode will operate:
- WCFClient 1 requests a method call onWCFService.
- AWCFService instance is created and the request is served.WCFService instance is not destroyed, the service instance is persisted to server other requests.
- Now let's say some otherWCFClient, e.g., client 2, requests a method call.
- The sameWCFInstance which was createdWCFClient 1 is used to serve the requestWCFClient 2. In other words, only one globalWCFServer service instance is created to serve all client requests.
How to implement Single Instance Mode
In order to create a single instance ofWCFService, we need to specifyInstancecontextmodeAsSingle.
[Servicebehavior (instancecontextmode =Instancecontextmode. Single)]Public ClassService: iservice {}
If you callWCFService 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 wowould like to have a scaled out architecture.
- YourWCFFunctions are called in a single threaded model.
Per session
- You want to maintain StatesWCFCILS.
- You a scaled up architecture.
- Light resource references.
Single
- You want share global data through yourWCFService.
- Scalability is not a concern.
References
- Msdn linkWCFInstances: http://msdn.microsoft.com/en-us/library/ms733040.aspx.
- Do not miss this post which covers end to end aboutWCFSessions: http://codeidol.com/csharp/WCF/Instance-management /.
- Great blog by Rick rain onWCFInstancing: 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 fromHere.
Necessary instructions
This is a very clearn and logical article posted on codeprojectShivprasadKoirala ,8 Jun 2010.as it'sVery 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.
Other articles written Shivprasad On WCF
You can refer to someHisOther articles written onWCF:
-
- WCFFAQ Part 1: http://www.codeproject.com/KB/aspnet/WCF. Aspx
- WCFFAQ Part 2 http://www.codeproject.com/KB/aspnet/WCFPart2.aspx
-
- WCFFAQ Part 3:WCFFaqpart3.aspx
-
- WCFFAQ Part 4:WCFFaqpart5.aspx
-
- WCFTransactions:WCFTransactions. aspx
-
- WCFInstancing:WCFInstance. aspx
-
- Bindings:WCFBasichttpbinding. aspx
-
- Httpbinding. aspx
- WCF concurrency (single, multiple, and reentrant) and throttling ------------------------------------------------------ the birthday of debuglzq on January 1! Happy birthday to myself! de: alles gute zum geburtstag!
method: Joyeux anniversaire!
stick: Getting Started without getting started!
Japan: birthday Greetings!
world: Happy birthday! & merry Christmas to you everyone! best wishes!