Windows Communication Foundation - SPLessons

WCF Instance Management

Home > Lesson > Chapter 15
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

WCF Instance Management

WCF Instance Management

shape Description

The contract session mode and the service instance mode have a permanent effect on the behavior of the client calls, the way the calls are played back to the service, and reputation on the overall program workflow and allowed assumptions. 

Per-Call Service

shape Description

Per-Call is used to create a new object for every client request, irrespective of whether the request comes from the same client or a different client. For every call, a new instance service is created which also mean indirectly that service instance get destroyed right away after the request fulfill.

shape Example

Create a simple WCF service with the name as "WCFServicewithInstancemanagemet" and change the service name as the instance.Write the following code in Instance.cs. [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServicewithInstancemanagement { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IInstance" in both code and config file together. [ServiceContract] public interface IInstance { [OperationContract] int increment(); } } [/csharp] Write the following code into Instance.cs file like below: [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServicewithInstancemanagement { [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)] public class Instance : IInstance { int num; public int increment() { num = num + 1; return num; } } }[/csharp] Now, simple WCF Service is ready. Build the service for getting the assembly file from the service(.dill file). Now create a Self-Host project with the Console application for hosting the service on the same solution and change the name as "InstanceHost". Then add the service references which is in WCF Service and system.Servicemodal namespace assembly and write the following code into App.config file like below: [csharp] <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> <services> <service behaviorConfiguration="mexBehavior" name="WcfServicewithInstancemanagement.Instan"> <endpoint address="WcfServicewithInstancemanagement.Instance" binding="netTcpBinding" bindingConfiguration="" contract="WcfServicewithInstancemanagement.IInstance" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080" /> <add baseAddress="net.tcp://localhost:8090" /> </baseAddresses> </host> </service> </services> </system.serviceModel>[/csharp] Write the following code in Program.cs file: [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceModel; namespace InstanceHost { class Program { static void Main(string[] args) { using(ServiceHost host= new ServiceHost(typeof(WcfServicewithInstancemanagement.Instance))) { host.Open(); Console.WriteLine("Service hosted time :" + DateTime.Now); Console.ReadLine(); } } } } [/csharp] Now, set startup project as InstanceHost project and run.Then the result is obtained like below: Now the service has successful running. Then take one more console application project for consuming client and change the name as Precallclient. Then add the service reference which is currently running service (Above service). And write the following code on program.cs. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PrecallClient { class Program { static void Main(string[] args) { Istanceservice.InstanceClient client = new Istanceservice.InstanceClient(); int num=client.increment(); Console.WriteLine("After first request =" + num); num = client.increment(); Console.WriteLine("After second request =" + num); num = client.increment(); Console.WriteLine("After third request =" + num); num = client.increment(); Console.WriteLine("After fourh request =" + num); num = client.increment(); Console.WriteLine("After fifth request =" + num); Console.ReadLine(); } } } [/csharp] The result will be like below: per-call service can be configured as follows: [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]

Per-Session Service

shape Description

Session means it maintains the state of the particular time for the client requests. Here pre-session means, a new object is created for each new client session and maintained for the duration of that session. Here, take the same WCF Service, host project and Client application as mentioned above. Only change the per-call service configured to Pre-Session like below: [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)] Then the result will be like below:

Single Service

shape Description

Single service is used to maintain all the requests lifetime, irrespective of whether it is coming from one client or different clients. The main theme of the single service is to maintain all the requests lifetime and remaining all the things are same as Per-Session. Single service can be configured as follows: [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)].