Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

.NET Remoting Tutorial

Different applications can communicate with one another through a .NET Remoting. This is accomplished through remote processes, which the .NET objects can make use of. It is to note that while using .net remoting, it is not essential that your applications which intend to communicate are located on the same computer, they can work with different computers on the same network or across different networks.

 

.NET Remoting along with DCOM and web services are a part of the distributed applications. .NET Remoting is actually communication between server and client objects using object references. It is to note that to use .NET Remoting, a client needs to be built using .NET.

Difference between ASP.NET Web services and .NET Remoting

1. Web services can only be accessed over HTTP whereas .NET Remoting can be accessed over various protocols like TCP, HTTP etc.

2. Web services operate in a stateless environment since its HTTP, a stateless protocol whereas .NET remoting support state management (as in through Singleton and SingleCall objects)

3. Web services are more reliable than .NET Remoting.

4. Web services are easy to create and use while .NET Remoting are complex  to be created.

5. Web services support heterogeneous environments i.e. support interoperability across platforms, on the other hand .NET remoting requires client to be built using .NET, thus it does'nt support heterogeneous environment.

6. Web Services support datatypes defined in the XSD type system while .NET remoting provides support for rich type system using binary communication.

Channels and Remote Object

Now that you know what exactly  .NET Remoting is, we will study on working with how this is implemented. First requirement is a Channel, i.e. the medium through the which the messages would be transferred. The remote objects are to communicate with one another and channels do this transport job of communication. The two existing channels are HttpChannel and TCPChannel.

We have three types of that can be used as a Remote Object. They are:

  • Server Activated Objects: They are of two types:

    • Single Call: They are those objects that can service only one request..

    •  Singleton Objects: These objects share data service multiple clients.

  • Client-Activated Objects: They are the ones which are activated once they get a call from the client.

Implementing .NET Remoting

To accomplish this, we will need:

  • Creating a remote object ( a type and a host application domain)

  • A client application domain

Creating a Remote Object

To create an object, we must declare a remotable class. It is to note that the class has to inherit the MarshalByRefObject. To do this, Open a new project of Class library type. and write the following code, to calculate product of 2 numbers:

using System;
 
namespace addsubs
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class addsubs : MarshalByRefObject
    {
        public int product;
        public int multiply(int a, int b)
        {
            product = a * b;
            return product;
        }
    }
}

Now build the project and you will have a DLL generated.

Now to create object of this class remotely, we must build a host application which registers our created class and a channel for remoting. Create it in the same directory where you have addsubs.dll. And compile it to dll our code will be using host.exe.config, so we will need to create it too in the same directory as host.exe. The host.exe.config is as follows:

<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown mode="Singleton" type="addsubs, addsubs" objectUri="addsubs.rem" />
</service>
<channels>
<channel ref="http" port="8989"/> <!--if you want tcp then port would be =8080-->
</channels>
</application>
</system.runtime.remoting>
</configuration>

using System;
using System.Runtime.Remoting;
 
namespace host
{
    public class host
    {
        public static int Main(string[] args)
        {
            RemotingConfiguration.Configure("host.exe.config");
            Console.WriteLine("Your requests...... Press Enter to exit");
            Console.ReadLine();
        }
    }
}

A Client application domain

Our application must reside in the client application domain to use .NET Remoting system. For this we will create a client.cs and client.exe.config, as we created for the host domain.

<configuration>
  <system.runtime.remoting>
    <application>
      <client>
        <wellknown type="addsubs, addsubs" url="http://localhost:8989/addsubs.rem" />
      </client>
    </application>
  </system.runtime.remoting>
</configuration>

Save the above as client.exe.config. Keep a note on the directory where you save these files. You should save the client.exe and the config file in the same directory.

using System;
using System.Runtime.Remoting ;
 
namespace client
{
    public class client
    {
        public static int Main(string[] args)
        {
            RemotingConfiguration.Configure("Client.exe.config");
            addsubs var = new addsubs();
            Console.WriteLine("Enter a Number");
            String x = Console.ReadLine();
            Console.WriteLine("Enter another Number");
            String y = Console.ReadLine();
            String z = var.mutiply(Convert.ToInt32(x), Convert.ToInt32(y)).ToString();
            Console.WriteLine(z);
        }
    }
}

To test your application run the client.exe from the command prompt.

It is to note that creating a .config file is not essential, you have an alternative method to include the config settings in your .cs file itself as ahown below. Remember to include the following two namespaces.

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

Next, include these two lines of code in the host.cs and client.cs files along with the other functionality as discussed above:

HttpChannel channel = new TcpChannel(8989);
ChannelServices.RegisterChannel(channel);


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   


comments powered by Disqus