.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

Single Instance Management in WCF

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

This article will explain about Singleton Service Instance mode. This article will give theoretical introduction and explanation of coding sample also. It will also discuss about various disadvantages of Singleton Service Instance mode and its potential place of uses.
 

Objective


This article will explain about Singleton Service Instance mode. This article will give theoretical introduction and explanation of coding sample also. It will also discuss about various disadvantages of Singleton Service Instance mode and its potential place of uses.


Singleton Service

InsMgmt1.gif

InsMgmt2.gif
 
  1. The Singleton Service Instance created only once.
  2. It created when the service host is created.
  3. It lives forever.
  4. It got disposed only when host shuts down.
  5. Singleton service instance does not require client to maintain any logical session.
  6. It does not required to use binding that supports transport level session.

    Let us suppose client is consuming a contract, which is having session then during the service call the Singleton service instance will have the same Session Id as of the client. When client will close the proxy , that  will terminate only the Transport session not the Singleton service instance.
      Whereas, if the client is consuming a contract, which does not have session. That client will also get connected to the same shared singleton service instance.


Code

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

Explanation with example

Service Contract 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

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

        [OperationContract]
        int GetDataFromRequiredContract();      
    }     
}

Service Contract 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace SingleServiceInstanceService
{

    [ServiceContract(SessionMode = SessionMode.NotAllowed)]
    public interface IService2
    {

        [OperationContract]
        int GetDataFromNotAllowedContract();


    }
}


Service 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace SingleServiceInstanceService
{
   [ServiceBehavior(InstanceContextMode =InstanceContextMode.Single)]
    public class Service1 : IService1,IService2 ,IDisposable 
    {

       int _Counter = 0;
       public Service1()
       {
       }
       public int GetDataFromRequiredContract()
        {
            _Counter++;
            return _Counter;
        }

      public int GetDataFromNotAllowedContract()
        {
            _Counter++;
            return _Counter;
        }

      public void Dispose()
      {
      }

    }
}
 
  1. There are two contracts IService1 and IService2.
  2. For IService1 session mode is set to SessionMode=SessionMode.Required
  3. For IService2 session mode is set to

    SessionMode = SessionMode.NotAllowed
     

  4. Service is implanting both the contracts.
  5. Service Behavior is configured to single instance mode

    ServiceBehavior(InstanceContextMode  =InstanceContextMode.Single)]
    Web.Config

     
      
        
          
            
            
              
              
            
    
            
    
            
            
            
            
          
        
        
          
            
                        
              
            
          
        
      

  6. In System.ServiceModel, we need to expose two addresses. One each for individual contracts.
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 proxy = new Service1Client();
            Console.WriteLine(proxy.GetDataFromRequiredContract().ToString());
            Service2Client proxy2 = new Service2Client();
            Console.WriteLine(proxy2.GetDataFromNotAllowedContract().ToString());
            Console.ReadKey(true);
            
        }
    }
}


Output

InsMgmt10.gif

Explanation of output

Since service instance is configured for Singleton, all the proxy will share the same service instance. That is why all proxy is incrementing the same value. And as the output, each time client is getting incremented value.

Disadvantage
  1. Having scalability problem.
  2. All requests to service will run on different worker thread, so it will cause synchronization problem.
  3. Only one client could have access to singleton service instance at a time. It will decrease throughput of the service.
  4. Responsiveness is very low
  5. Singleton Service instance should only be used, when no other options are available. For instance Global Log Book, Single Communication Port etc.
Conclusion

This article explained about, Singleton Service Instance mode. This article gave theoretical introduction and explanation of coding sample also. It also discussed about various disadvantages of Singleton Service Instance mode and its potential place of uses. 

 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