.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

Instance Management in WCF Tutorial

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

In this Tutorial we will see the Instance Management in WCF. Instance Management is a technique to decide, which SERVICE INSTANCE handles which CLIENT REQUEST and when?
 

Instance Management is a technique to decide, which SERVICE INSTANCE handles which CLIENT REQUEST and when? This is a service side implementation detail. This is useful to decide scalability, performance, durability, transactions and queued calls.

Instancemanagement1.gif

There are three instance management techniques in WCF.

Instancemanagement2.gif


Instancemanagement3.gif

BEHAVIOR

Instance management is a service side activation implementation detail that should not get manifested to the client. WCF achieve this by BEHAVIOR.
  1. A Behavior is a local attribute of service that does not affect its communication patterns.
  2. Client does not aware of the Behavior.
  3. Service does not manifest behavior in binding or metadata.
  4. Practically Behaviors are WCF classes.
  5. These are used at the runtime operations.

    Instancemanagement4.gif
SERVICE BEHAVIOR
  1. This applies directly to service implementation class.
  2. This affects all the endpoints.
  3. This is used to configure service instance mode.
OPERATION BEHAVIOR
  1. This is used to configure operation behavior.
  2. This affects only implementation of a particular operation.
  3. This could apply only to method that implements contract operation.
  4. This is not applied to the contract definition itself.
Explanation of Service Behavior attribute
  1. ServiceBehaviorAttribute class is used to configure the instance context mode.
  2. As we can see in highlighted text below that InstanceContextMode is a public property.
  3. Type of InstanceContextMode property is enum InstanceContextMode.
  4. It is inside namespace System.ServiceModel.

    Instancemanagement5.gif

Explanation of Instance Context Mode
  1. This is a enum type
  2. This type is defined under namespace System.ServiceModel.

    using System;
    
    namespace System.ServiceModel
    {
        
        public enum InstanceContextMode
        {
            
            PerSession = 0,
            
            PerCall = 1,
            
            Single = 2,
        }
    }


    Instancemanagement7.gif

Per-Call Services

Every Client request gets a new dedicated service instance. A service instance exists only when a client call is in progress.

Instancemanagement8.gif

Instancemanagement9.gif

Steps of Per-Call Service activation

Instancemanagement10.gif

After step 6, WCF calls the Dispose() method, if the service is implementing IDisposable interface. Dispose method is being called on the same thread, which dispatches the original method call. Once Dispose() called , WCF disconnects service instance from rest of the WCF architecture and makes service instance a candidate for Garbage collection.


Example of Per-Call Service

Creating service
  1. Create a WCF service application.
  2. In Service Contract, create an Operation Contract. This method (Operation Contract) will return an Integer.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    namespace PerCallsample
    {   
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
             int MyMethod();     
        }   
    }

  3. Create a Service class.
    a. This class will have Service Behavior Attribute.
    b. Service Behavior Attribute is configured for Per- Call Instance management.
    c. InstanceContextMode property of Service Behavior class takes Per-Call value for enum type InstanceContextMode Type. ( See highlighted text in below code)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    
    namespace PerCallsample
    {
    [ServiceBehavior(InstanceContextMode =  InstanceContextMode.PerCall)]
        public class Service1 : IService1,IDisposable 
        {
         static   int _callCounter = 0;
    
            public Service1()
            {
                
                _callCounter = _callCounter + 1; 
            }
            public int  MyMethod()
            {
               return  _callCounter;
               
            }
            public void Dispose()
            {
                _callCounter = _callCounter - 1;
                
            }
        }
    }

Explanation of code
  1. I have taken one static variable because its value will persist across all service instances.
  2. In constructor of service class, I am incrementing value of this variable by one.
  3. In Operation contract, I am returning value of variable to client.
  4. On Dispose of service instance, I am again decrementing value of variable by one.
Creating client
  1. Add a Console application in same project as client.
  2. Add service reference of service.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using PerCallClient.ServiceReference1;
    
    namespace PerCallClient
    {
        class Program
        {
            static void Main(string[] args)
            {
        Service1Client proxy = new Service1Client();
               int x=  proxy.MyMethod();
               proxy.MyMethod();
               Console.WriteLine(x.ToString());
               Console.Read();
               int y = proxy.MyMethod();
               Console.WriteLine(y.ToString());
               Console.Read();
               proxy.Close();
            }
        }
    }
Output

Instancemanagement14.gif

Explanation of output

From client, service method is being called twice. But since in Per-call Service activation, separate service instance is being created for separate method call and after returning from method dispose is being called on the same thread. So value of static variable is remaining same.

Let us take a try. Just go ahead and comment ServiceBeahavior attribute from service class

So, now service class will look like

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

namespace PerCallsample
{
   //[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class Service1 : IService1,IDisposable 
    {
     static   int _callCounter = 0;

        public Service1()
        {
            _callCounter = _callCounter + 1; 
        }

        public int  MyMethod()
        {
           return  _callCounter;
        }

        public void Dispose()
        {
            _callCounter = _callCounter - 1;       
        }
    }
}


Just recompile the service and update the service reference. And run the console application. (At least 5 times) see the output. You will find each time; you are running you are getting increased value. But if service is configured for Per-Call instance management, each time you are getting the same output that is 1.


Conclusion

I have given introduction of Instance management is WCF. I also explained One Instance management technique.

Future Scope


In next articles of this series, I will explain about Per-Session Instance management technique and Single Instance management technique.

Till then, Happy Coding.

 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