Creating a remoting service and a web service - Chapter- 2

Introduction:
Creating a .Net Remoting Service & a Web Service
In this article we Will Learn.

Creating a Simple Remoting Service and a Web Service
Along the way we will get a feel how different these two technologies are.

First client requirement:
Creating a remoting service and a web service
 Let’s assume we have got two clients. First client is using a java application to communicate with our web service. So the main requirement here is operability. To make our service interoperable we gone exchange message in xml formats and we gone use http protocol. XML over http offers interoperability so to make our service interoperable we going to build service as a web service.

Second client requirement:
Creating a remoting service and a web service - Chapter- 2
Second client is using a windows application to communicate with our web service. Windows application is also a dot net application. The main requirement for this client is performance. To better performance we gone exchange message in binary format over TCP protocol because binary over TCP offers better performance. So here we are going to rewrite the same service using dot net remoting technology.

So let’s first of all go ahead and implement a web service. Let’s flip to Visual Studio.
Creating a remoting service and a web service - Chapter- 2
Creating a remoting service and a web service - Chapter- 2
In to this project let’s have a web service.
Creating a remoting service and a web service - Chapter- 2
Creating a remoting service and a web service - Chapter- 2
We have done implementing the web service. Let’s now view this web service in browser.
Creating a remoting service and a web service - Chapter- 2
Now click on Get Message method.
Creating a remoting service and a web service - Chapter- 2
Now enter text in textbox and click on invoke button.
Creating a remoting service and a web service - Chapter- 2
Creating a remoting service and a web service - Chapter- 2
Now we have getting response back Hello vepsh. So it working as expected.

Now the client is going to be a java web application but then I don’t have java installed on my machine. But web services are interoperable so a java application can communicate with web service that we have built.

Let’s test our services using an ASP.Net web application. And to do that add a new project in solution.
Creating a remoting service and a web service - Chapter- 2
Now add a Web form.
Creating a remoting service and a web service - Chapter- 2
Now design the web from.
Creating a remoting service and a web service - Chapter- 2
After design Page look like:
Creating a remoting service and a web service - Chapter- 2
Code for consuming web service
Creating a remoting service and a web service - Chapter- 2
Now let’s run our web from.
Now the web application able to communicate with service

Now go ahead and build dot net remoting service to satisfy the requirement of second client.

Now open visual studio and I am going to add project of type class library.
Creating a remoting service and a web service - Chapter- 2
 Now add class library project
Creating a remoting service and a web service - Chapter- 2
Creating a remoting service and a web service - Chapter- 2
So this class library project is just going to contain one interface. Let’s change the name of this class to IHelloRemotingService.
In a bit you will understand why we creating this interface.
Creating a remoting service and a web service - Chapter- 2
It’s a same service we are just re writing it using remoting technology.

Now next is to implement the remoting service itself .And to do that to i am going to add new project in this solution.
Creating a remoting service and a web service - Chapter- 2
Creating a remoting service and a web service - Chapter- 2
 Now let’s change the name of this class to HelloRemotingService.
Creating a remoting service and a web service - Chapter- 2
Now to make this HelloRemotingService class remotable I am going to make this inherit from MarshalByRefObject. And we also need to make this class inherit from IHelloRemotingService. To do that we need to add reference from IHelloRemotingService project to HelloRemotingService project.
Creating a remoting service and a web service - Chapter- 2
That’s it we have done implementing the service.
Now we need to host this remoting service.
There are several choses to host remoting service.
1-    Console Application
2-    Windows from Application
3-    Windows service
4-    remoting service  can also be hosted in IIS
Let’s host it in console application. So let’s add console application to the solution.
Creating a remoting service and a web service - Chapter- 2
Now the RemotingServiceHost console project need to have reference of  HelloRemotingService and IHelloRemotingService. So let’s add reference.
Creating a remoting service and a web service - Chapter- 2
Now another thing that we need to do is since we are do remoting here. We need to add reference to System.Runtime.Remoting  

Now we are going to TCP protocol to communicate.

Now within the main function of this class we need to host the service.  When we run this the main method is going to run at that point you know the service will be hosted.

The first step to create the service class itself.

So what is the service class HelloRemotingService which is present in HelloRemotingService namespace.

Now with in our console application let’s go and create an instance of that HelloRemotingService class. 

The next step to create TCP channel for communication

Let’s have the complete set code.

using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RemotingServiceHost
{
    class Program
    {
        static void Main(string[] args)
        {
            HelloRemotingService.HelloRemotingService objRemotingService = new 
                HelloRemotingService.HelloRemotingService();

            TcpChannel objTcpChannel = new TcpChannel(8080);
            ChannelServices.RegisterChannel(objTcpChannel);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(HelloRemotingService.HelloRemotingService), 
                                      "GetMessage", WellKnownObjectMode.Singleton);

            Console.WriteLine("Remoting service started @" + DateTime.Now);
            Console.ReadLine();
        }
    }
}

Now at this point we have hosted our remoting service as well so all that left now is to consume this remoting service. We have decided the client is going to be a windows application.

So let’s go ahead and a windows application in to the solution.
Creating a remoting service and a web service - Chapter- 2
 Now design the windows from like:-
Creating a remoting service and a web service - Chapter- 2

using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelloRemotingServiceClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

     private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

Now we need to consume the remoting service that we have implemented. This client project needs to have a reference to remoting assembly itself. So let’s go and add remoting assembly. And we need a reference to interface assembly as well to the client we should not be giving the implementation details. We should only let him know about the interface that his going to communicate with. So that’s the reason why we have separated the service interface from its implementation. 

So let’s go and reference to that project.
Creating a remoting service and a web service - Chapter- 2
Now the first thing here at the class level let’s creates a variable of type IHelloRemotingService which is present in the namespace IHelloRemotingService and the interface itself is IHelloRemotingService.

Now when the form is created what we need to do we need open up a TCP channel and then communicate with the service. So the first step here is to create a TCP channel.

using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace HelloRemotingServiceClient
{
    public partial class Form1 : Form
    {
        IHelloRemotingService.IHelloRemotingService objClient;
        public Form1()
        {
            InitializeComponent();
            TcpChannel objTcpChannel = new TcpChannel();
            ChannelServices.RegisterChannel(objTcpChannel);
objClient = (IHelloRemotingService.IHelloRemotingService)Activator.GetObject
 (typeof(IHelloRemotingService.IHelloRemotingService), 
"tcp://localhost:8080/GetMessage");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = objClient.GetMessage(textBox1.Text);
        }
    }
}

Now we have done. Let’s build the solution to make sure everything working as expected.
1-  Now need to first run the host. So let’s run the RemotingServiceHost Priject.
Creating a remoting service and a web service - Chapter- 2
 Look at that now service is running.
2-
Now we need to run the client itself. So let’s run the HelloRemotingServiceClient Project.
Creating a remoting service and a web service - Chapter- 2
Now enter any text on textbox. Let’s enter Vepsh in textbox and click on the button Get Message. So this should invoke the service and we get the response.
 
Creating a remoting service and a web service - Chapter- 2

So this is a very simple service but look at the amount of code that we have write. And how different is the implementation from that of Web Service. It’s a same service but technology is different from Web Service and that is the reason why it’s so different implementing it.