Tip

Remoting 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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.