Blogger Templates

Thursday, August 22, 2013

0

Implementing Instance modes using WCF

Posted in , ,
(In this demo I will be using visual studio 2012 and hence the features/options/methods used here might vary based on your version models)


Part I)Creating a Service:

1)Select New Project -> WCF -> WCF service library in Visual Studio 2012

2)Creating a new service library will open a solution with interface(IService1.cs) and a class(Service1.cs).
3)Modify the interface with the methods that are required. Here I have created a method getdataafterincrement()
4)Implement the interface methods in the class file as follows

5)Define the necessary functional logic.
The IService1.cs has the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace wcfinstance
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        int getdataafterincrement(int value);      
    }
}

The Service1.cs has the following code.
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace wcfinstance
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class Service1 : IService1
    {

        int dataid = 100;
        public int getdataafterincrement(int value)
        {
            dataid++;
            return dataid;
        }
    }
}
The above service can be tested by running it in the inbuild Microsoft wcf test client. In the following image you can notice the response value as 101.


Part II)Creating a Client

1)Add a new console application project in the same solution.
2)Run the WCF test client (or) the user created host without debugging and add the service reference to the endpoint base address.[Note:The base address can be found in the App.config file of the service project]

3)Create a proxy in the console application and access the service methods required through that proxy.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wcfclient.ServiceReference1;
namespace wcfclient
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1Client proxy = new Service1Client();
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(proxy.getdataafterincrement());
            }
        }
    }
}


Part III) Creating a Host

1)Host can be created by the user itself. Firstly add a new console application on to the same solution.
2)Add a reference to the class(wcfinstance) of the service and to the system.servicemodel assembly.

3)Move the App.config file from service to the host.
4)Implement the code logic of the host as follows.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace wcfhost
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost svh=new ServiceHost(typeof(wcfinstance.Service1));
            svh.Open();
            Console.WriteLine("Service Running...");
            Console.ReadLine();
        }
    }
}
The final solution explorer look as follows

Running Procedure:

1) Set the host project as the startup project and start the host without debugging.
2) Set the client project as the startup project and start it without debugging.

Outcomes based on Instance context mode:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
This one creates a single instance for every single call. The endpoint binding in App.config is set to “basicHttpBinding”


[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
This one creates a single instance for a single session. The endpoint binding in App.config is set to “wsHttpBinding”


[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
This one creates a single instance altogether. The endpoint binding in App.config is set to “wsHttpBinding”

If this post helped you then do not forget to subscribe. Feel free to drop your comments.

0 comments: