Trek Innovations

Thoughts For You

Instance Management using WCF

Instance Management using WCF: 

Below given example make clear the instancing behaviour setting, which controls how instances of a service class are created in response to client requests.

Types of instancing modes:

PerCall:  A new instance is created for each client request.

PerSession:  A new instance is created for each new client session, and it will be available throughout the session

Single: Only one instance handles for all client requests throughout the application 

 The service class specifies instancing behaviour as [ServiceBehavior(InstanceContextMode=<Instance_Mode>)].<instance_Type>.

//PerSession: A new instance is created for each new client session, and it will be available throughout the session

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)] 

//PerCall:  A new instance is created for each client request.

//[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 

//Single: Only one instance handles for all client requests throughout the application //[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 

The service class contains the following

1.    Constructor, which is useful to create new GUID for every instance of service class

2.     getInfo(), returns message to the client 

Implementation Steps follows as below

1)    Create new WCF Service Library Project with the name WCFLib

2)    Rename Iservice.cs to IServicedClass.cs from Solution Explorer

3)    Rename Service.cs to ServicedClass.cs from Solution Explorer

4)    Add Reference to System.ServiceModel from Solution Explorer

5)    Write the following code in IServicedClass.cs 

using System.ServiceModel; 

namespace WCFLib

{

    [ServiceContract]

    public interface IServicedClass

    {

        [OperationContract]

        string getInfo();

    }

}

 

6)    Write the following code in ServicedClass.cs 

using System.ServiceModel;

using System.Threading; 

namespace WCFLib

{

    /* Instance Management Begin */ 

    //PerCall instance

   // [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)] 

   //PerSession instance

   // [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)] 

   //Single instance

   [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]  

   /* Instance Management End */ 

    public class ServicedClass:IServicedClass

    {

        private Guid id;

        public ServicedClass()

        {

            id = Guid.NewGuid();

        } 

        public string getInfo()

        {

            Thread.Sleep(2000);

            return “*** getInfo called on Object id : “ + id.ToString() + “***”;

        }

    }

} 

Note: Rebuild the service (WCFLib project) after changing the instancing mode.  

7)    Build only WCFLib project

8)    Add new Console Application with the name  WCFSerCon in the solution

9)    Add Reference to System.ServiceModel

10) Add Reference to WCFLib.dll

11) Write the following code in Program.cs 

using System.ServiceModel;

using WCFLib; 

namespace WCFSerCon

{

    class Program

    {

        static void Main(string[] args)

        {

            ServiceHost host = new ServiceHost(typeof(ServicedClass));

            string address = “net.tcp://localhost:1099/Myobject”;

            NetTcpBinding binding = new NetTcpBinding();

            host.AddServiceEndpoint(typeof(IServicedClass), binding, address);

            host.Open();

            Console.WriteLine(“Server is Ready and Running…”);

            Console.ReadLine(); 

        }

    }

} 

12) Build only WCFSerCon project

13) Add new Console Application with the name  WCFClientCon in the solution

14) Add Reference to System.ServiceModel

15) Add Reference to WCFLib.dll

16) Write the following code in Program.cs 

using System.Threading;

using System.ServiceModel;

using WCFLib; 

namespace WCFClientCon

{

    class Program

    {

        static void Main(string[] args)

        {

            string address = “net.tcp://localhost:1099/Myobject”;

            NetTcpBinding binding = new NetTcpBinding();

            ChannelFactory<IServicedClass> cf = new ChannelFactory<IServicedClass>(binding, address);

            IServicedClass obj = cf.CreateChannel();        

            //Instance Management

            for (int i = 0; i < 5; i++)

            {

                Console.Write(DateTime.Now.ToLongTimeString());

                Console.WriteLine(obj.getInfo());

                Console.WriteLine();

                Thread.Sleep(1000);

            } 

            Console.WriteLine();

            Console.WriteLine(“Press any key…”);

            Console.ReadLine(); 

        }

    }

} 

17) Build only WCFClientCon project

18) Set WCFSerCon as start up project and Run using ctrl+F5

 

19) Now server is ready and you can find the following screen 

 

 

 Image 1 

  Output window

 

In main(), we calling getInfo(), which is defined in WCFLib/ServicedClass.cs, five times using loop.

  

In the above output we are getting same IDs. i.e. whenever we are calling a function, it’s not creating new object. This is happening because of the following service behaviour in class, WCFLib/ServicedClass.cs

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 

22) Instead of the above Service behaviour, if we will write the following statement in same file, WCFLib/ServicedClass.cs 

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

 

we can get the following output 

 

 

 

 

 

In this case we are getting different IDs. i.e. whenever we are calling a function, it’s creating new object and calling constructer, which is generating new ID.

Share This Post No comments

No comments yet. Be the first.

Leave a reply