SIGN UP MEMBER LOGIN:    
ARTICLE

Remoting in .NET

Posted by Manisha Mehta Articles | Internet & Web April 05, 2001
.NET Remoting provides a way for application in different machines/domains to communicate with each other.
Reader Level:

.NET Remoting provides a way for application in different machines/domains to communicate with each other. Remoting provides a powerful yet an easy way to communicate with object in different app domains. Any object which 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 thru the channel viz. HTTP or TCP.

I have also used the utility SOAPSUDS.EXE which is used to generates proxies for the remote component. The Following example will help us understand Remoting mechanism in .NET. I have preferred to chose C# over VB.NET in 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.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);
RemotingServices.RegisterWellKnownType ("Server","Server.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 and on the console u should see

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


To check wether the HTTP channel is binded to the port open the browser and type http://localhost:1095/ServiceClass

You should see a XML file describing the Service.

Step 2: Creating Client Proxy and Code on Machine2

Creating a client proxy requires to use a tool provided by Microsoft 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://<Machine Name >:1095/ServiceClass -oa:Server.dll 

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

Client Code TheClient.cs

using System;
using System.Runtime.Remoting;
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://<Machine where Service is Running >: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

Login to add your contents and source code to this article
share this article :
post comment
 

Hi, I am working with vb.net remoting. Now before calling server's method I want to sense/listen whether server is available/responding or not so it will not throw me an error. Help me out. thank you

Posted by Divyesh Chapaneri Jan 04, 2008

I didn't get how to execute soapsuds.exe and how to get xml file in other machine from Manjula Manjula.Parameshwara@in.unisys.com

Posted by Manjula Sumanth Sep 06, 2007

how can i use this program to send files from a computer to another instead of text messages?

Posted by Mihai si atat Jan 05, 2006
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor