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

 


 

  1. WCF can maintain a logical session between a client and service.
  2. The service instance remains in the memory throughout the session.
  3. 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

  1. The Behavior part is required, so that WCF will keep the Service Instance Context alive throughout the session.

 

 

  1. InstanceContextMode.PerSession is default value for InstanceContextmode property.
  2. The session terminates when client closes the proxy.
  3. 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.

 

 

  1. For this ServiceContract attribute having a property called SessionMode.
  2. Type of property SessionMode is enum SessionMode.
  3. It is inside namespace System.ServiceModel.
  4. Property SessionMode is a public property


 

  1. SessionMode enum is default to SessionMode.Allowed.
  2. The Configured value of SessionMode is exposed to client in MetaData.

SessionMode.Allowed

 

  1. When the SessionMode is configured with SessionMode.Allowed, transport session are allowed but not enforced.
  2. The Behavior of service will depend upon behavior configuration and binding.  


Code Example

Both code is same because SessionMode.Allowed is default.


 

SessionMode.Required

 

  1. SessionMode.Required value restricts to use Transport level session. 
  2. 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 

  1. This disallows the use of Transport session.
  2. 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

  1. We could see from output is that Service Instance is particular and individual for each individual proxy.
  2. 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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s