.NET Remoting : How to create a Client To Use the Remotable Object?

Create a new C# console application project. Add a class called SampleClient and paste in the following code. Add a reference to System.Runtime.Remoting in the project as well as a reference to the project containing the MyObject.


using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using MyRemoting
namespace MyRemotingClient
{
   /// Sample client to demonstrate the use of .NET Remoting.

   public class SampleClient  
   {
       public static int Main(string [] args)    
       {
           // Create a channel for communicating with the remote object

           // Notice no port is specified on the client

           TcpChannel chan = new TcpChannel();
           ChannelServices.RegisterChannel(chan);

           // Create an instance of the remote object

           MyObject obj = (MyObject) Activator.GetObject(
               typeof(MyRemoting.MyObject),
               "tcp://localhost:8080/HelloWorld" );

           // Use the object

           if( obj.Equals(null) )      
           {
               System.Console.WriteLine(
                   "Error: unable to locate server");      
           }
           Else      {   Console.WriteLine(obj.HelloWorld());      
           }
           return 0;    
       }  
   }
}

No comments:

Post a Comment