.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Per-Session Instance Management in WCF Tutorial Part 2

Posted By:Dhananjay Kumar       Posted Date: March 13, 2010    Points: 25    Category: WCF    URL: http://www.dotnetspark.com  

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.
 

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

WCFSession1.gif

WCFSession2.gif 
  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.

    WCFSession3.gif
Configuring Session Full service

There are three elements, which must be taken care while configuring a session full service.

WCFSession4.gif


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

    WCFSession5.gif

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
        public class Service1 : IService1,IDisposable
    

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

WCFSession7.gif 
  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. 

    using System;
    using System.Net.Security;
    
    namespace System.ServiceModel
    {
        
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
        public sealed class ServiceContractAttribute : Attribute
        {
            public ServiceContractAttribute();
    
            
            public Type CallbackContract { get; set; }
            public string ConfigurationName { get; set; }
            
            public bool HasProtectionLevel { get; }
            public string Name { get; set; }
            
            public string Namespace { get; set; }
            
            public ProtectionLevel ProtectionLevel { get; set; }
            
            public SessionMode SessionMode { get; set; }
        }
    }

     
  5. SessionMode enum is default to SessionMode.Allowed.
  6. The Configured value of SessionMode is exposed to client in MetaData.

    using System;
    
    namespace System.ServiceModel
    {
        
        public enum SessionMode
        {
            
            Allowed = 0,
            
            Required = 1,
            
            NotAllowed = 2,
        }
    }
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.

    If Service is configured as PerCall , it will behave as Per Call Instance Mode. Whereas, if service is configured as PerSession , it will behave as PerSession but if binding is not supporting session , it will behave as PerCall instance mode.

    Take an example service is configured as PerSession and binding is basicHttpBinding, which is a connection less binding then service cannot use Transport level session and it will behave as Per Call configured service.

    For another example Binding is WSHttpBinding and service is configured as session full service. It will act as Per-Call instance management service, if security and reliability of message is not enabled.
Code Example

Both code is same because SessionMode.Allowed is default .

[ServiceContract(SessionMode=SessionMode.Allowed)]
    public interface IService1

[ServiceContract]
    public interface IService1

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.

    WCFSession13.gif

    error while loading the service.

    Take an example service is configured as PerSession with Contract as SessionMode.Required and binding is basicHttpBinding, which is a connection less binding then service, cannot use Transport level session and it will throw error while service is loading.


Code Example

[ServiceContract(SessionMode=SessionMode.Required)]
    public interface IService1
{
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class Service1 : IService1,IDisposable
{
}


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.
    WCFSession16.gif
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

WCFSession21.gif

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 articles explained about Session Full Service Management. 

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend