Objective
This article is part # 2 of Instance Management in WCF. This article will explain Session Full Instance management service. This will explain different Session Mode at Contract level. This will explain Per-session service also. This article will be explaining Session Full Service with a code also.
Part # 1 of this series could be found Here
Per-Session Service
- WCF can maintain a logical session between a client and service.
- The service instance remains in the memory throughout the session.
-
The client session is per service endpoint per proxy.
Configuring Session Full service
There are three elements, which must be taken care while configuring a session full service.
Behavior Facet
- The Behavior part is required, so that WCF will keep the Service Instance Context alive throughout the session.
- InstanceContextMode.PerSession is default value for InstanceContextmode property.
- The session terminates when client closes the proxy.
-
Proxy has to notify the service that session has been closed and service calls the Dispose () method on worker thread.
Contract Facet
In order to create all messages from a particular client to a particular instance WCF needs to identify the client and this is done by TRANSPORT SESSION.
- For this ServiceContract attribute having a property called SessionMode.
- Type of property SessionMode is enum SessionMode.
- It is inside namespace System.ServiceModel.
- Property SessionMode is a public property
- SessionMode enum is default to SessionMode.Allowed.
- The Configured value of SessionMode is exposed to client in MetaData.
SessionMode.Allowed
- When the SessionMode is configured with SessionMode.Allowed, transport session are allowed but not enforced.
-
The Behavior of service will depend upon behavior configuration and binding.
Code Example
Both code is same because SessionMode.Allowed is default.
SessionMode.Required
-
SessionMode.Required value restricts to use Transport level session.
-
If a contract is configured as SessionMode.Required with a service end point who’s binding does not maintain a Transport level session a Run time error will encountered at loading of service.
Code Example
SessionMode.NotAllowed
- This disallows the use of Transport session.
-
Regardless of the service configuration, when SessionMode.NowAllowed is configured at contract, the service will always behave as Per-call service.
Code Example
[ServiceContract(SessionMode=SessionMode.NotAllowed)]
public
interface
IService1
{
}
Explanation of Per-Session service instance with a Sample
Consider code below
Service contract
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace PerSessionSample
{
[ServiceContract(SessionMode=SessionMode.Required)]
public
interface
IService1
{
[OperationContract]
int GetData();
}
}
Service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace PerSessionSample
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public
class
Service1 : IService1,IDisposable
{
int _counter = 0;
public Service1()
{
}
public
int GetData()
{
_counter++;
return _counter;
}
public
void Dispose()
{
}
}
}
Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Test.ServiceReference1;
namespace Test
{
class
Program
{
static
void Main(string[] args)
{
Service1Client proxy1 = new
Service1Client();
Console.WriteLine(” 1st Output by Proxy 1″);
Console.WriteLine(proxy1.GetData().ToString());
Console.WriteLine(” 2nd Output by Proxy 1″);
Console.WriteLine(proxy1.GetData().ToString());
Service1Client proxy2 = new
Service1Client();
Console.WriteLine(“1st Output by Proxy 2”);
Console.WriteLine(proxy2.GetData().ToString());
proxy2.Close();
Service1Client proxy3 = new
Service1Client();
Console.WriteLine(“1st Output by Proxy 3”);
Console.WriteLine(proxy3.GetData().ToString());
Console.WriteLine(” 2nd Output by Proxy 3 “);
Console.WriteLine(proxy3.GetData().ToString());
Console.WriteLine(” 3rd Output by Proxy 1 “);
Console.WriteLine(proxy3.GetData().ToString());
Console.ReadKey(true);
}
}
}
Output
Explanation
- We could see from output is that Service Instance is particular and individual for each individual proxy.
- When proxy is getting closed or down, service instance is also being disposed on worker thread.
Conclusion
This article was part# 2 of multi series Instance Management Article. This article explained about Session Full Service Management.
Happy Coding
Leave a Reply