Remoting in .NET

An example that will help you understand how the remoting mechanism works in .NET.

Let other users know how useful this tip is by rating it below. Got a tip or code of your own you'd like to share?

Submit it here!


.NET Remoting provides a way for application in different machines/domains to communicate with each other and offers a powerful yet easy way to communicate with objects in different app domains. Any object that executes outside the app domain can be considered as remote.

Remote objects are accessed via channels. Channels can be thought of as transport mechanism to pass messages to and from remote objects. All the method calls along with the parameters are passed to the remote object through the channel via HTTP or TCP.

I have also used the utility SOAPSUDS.EXE, which is used to generate proxies for the remote component. The following example will help you understand the remoting mechanism in .NET. I chose to use C# for this example. We will create a server that will have a method.

Step 1: Creating the server server.cs on machine1

using System;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
namespace Server 
{
   public class ServiceClass : MarshalByRefObject {
      public void AddMessage (String msg) 
  {
         Console.WriteLine (msg);
  }
}
public class ServerClass 
{
  public static void Main () 
  {
    HttpChannel c = new HttpChannel (1095);
    ChannelServices.RegisterChannel (c);
    RemotingConfiguration.RegisterWellKnownServiceType(typeof
    (ServiceClass),"ServiceClass",WellKnownObjectMode.Singleton);    
     Console.WriteLine ("Server ON at 1095");
     Console.WriteLine ("Press enter to stop the server...");
     Console.ReadLine ();
  }
 }
}

Save this file as Server.cs. Compile this file using:

csc /r:system.runtime.remoting.dll /r:system.dll Server.cs

This will generate a executable server.exe. Run this file. On the console you should see:

Server ON at 1095
Press enter to stop the server...

To check whether the HTTP channel is binded to the port, open the browser and type: http://localhost:1095/ServiceClass?WSDL.

You should see a XML file describing the service.

Step 2: Creating client proxy and code on machine2:

Creating a client proxy requires that you use a Microsoft tool called soapsuds.exe. This utility ready the XML description and generates a proxy assembly used to access the server. Go to a different machine and type in:

soapsuds -url:http:// :1095/ServiceClass?WSDL -oa:Server.dll

This will create a proxy called Server.dll that will be used to access the remote object.

Client code TheClient.cs

>

using System; 
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;  
using System.Runtime.Remoting.Channels.Http;
using Server;
public class TheClient 
 {
    public static void Main (string[] args) 
  {
      HttpChannel c = new HttpChannel (1077);  
ChannelServices.RegisterChannel (c);
      ServiceClass sc = (ServiceClass) Activator.GetObject (typeof(ServiceClass),
      "http://
 
  :1095/ServiceClass"); 
      sc.AddMessage ("Hello From Client"); 
  }
 }

 

Save this file as TheClient.cs. Compile it using:

csc /r:system.runtime.remoting.dll /r:system.dll /r:Server.dll TheClient.cs

The output will be TheClient.exe. Run it and check the server console on machine 1. You will see "Hello From Client."

This example used HTTP Channel to transport messages to remote components. Likewise TCP channel can also be used to achieve the same result. More information regarding .NET remoting can be found at http://msdn.microsoft.com/library/techart/hawkremoting.htm


Source: DotNetExtreme.com
This was first published in November 2002

Dig deeper on Web services and SOA implementations in the .NET Framework

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close