December 18, 2014
Hot Topics:

.NET Remoting

  • October 10, 2002
  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Channels

Remote objects are accessed through Channels. Channels physically transport the messages to and from remote objects. There are two existing channels TcpChannel and HttpChannel. Their names give away the protocols that they use. In addition, the TcpChannel or HttpChannel can be extended, or a new channel created if you determine the existing channels do not meet your needs.

Create a Remotable Object

A remotable object is nothing more than an object that inherits from MarshalByRefObject. The following sample demonstrates a simple class to expose the omnipresent hello world. This object exposes a single method HelloWorld that will return a string. The only values that can be returned from methods are the classes in the .NET Framework that are serializable such as string and DataSet. In addition, if you need to return a user-defined object then the object needs to be marked as serializable.

Create a new C# class library project. Add a class called SampleObject and put in the following code. Add a reference to System.Runtime.Remoting in the project, otherwise the TcpChannel will not be found. Compile the class to make sure you have everything correct.

using System;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;namespace CodeGuru.Remoting{  /// <remarks>  /// Sample object to demonstrate the use of .NET Remoting.  /// </remarks>  public class SampleObject : MarshalByRefObject   {   /// <summary>   /// Constructor   /// </summary>    public SampleObject()   {   }   /// <summary>   /// Return a hello message   /// </summary>   /// <returns>Hello world message</returns>   public string HelloWorld()   {     return "Hello World!";   }  }}

Create a Server To Expose the Remotable Object

We need to create a server object that will act as a listener to accept remote object requests. For this example we will use the TCP/IP channel. We first create an instance of the channel and then register it for use by clients at a specific port. The service can be registered as WellKnownObjectMode.SingleCall, which results in a new instance of the object for each client, or as WellKnownObjectMode.Singleton, which results in one instance of the object used for all clients.

It is not necessary to create the server listener if you are planning to use IIS. For obvious reasons, IIS only supports the use of the HttpChannel. Create a virtual directory for your application and then put code to register your service in the Application_Start event.

For our example, we'll go ahead and create a server listener in case you don't have IIS. Since the service needs to be bound to an available port, for our example I chose 8080, which is a port that I know to be unused on my computer. You may need to choose a different port depending upon what ports you have available. To see a list of the used ports on your computer open a command prompt and issue the command "netstat --a". It may produce a long listing so make sure the command prompt buffer sizes are set to allow scrolling. Compile the class to make sure you have everything correct.

Create a new C# console application project. Add a class called SampleServer and paste in the following code. Add a reference to System.Runtime.Remoting in the project, otherwise the TcpChannel will not be found. In addition, add a reference to the project containing the SampleObject, otherwise the code will not compile because it won't know how to find a reference to SampleObject.

using System;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;namespace CodeGuru.Remoting{  /// <remarks>  /// Sample server to demonstrate the use of .NET Remoting.  /// </remarks>  public class SampleServer  {   public static int Main(string [] args)    {    // Create an instance of a channel     TcpChannel channel = new TcpChannel(8080);     ChannelServices.RegisterChannel(channel);        // Register as an available service with the name HelloWorld     RemotingConfiguration.RegisterWellKnownServiceType(          typeof(SampleObject),          "HelloWorld",          WellKnownObjectMode.SingleCall );     System.Console.WriteLine("Press the enter key to exit...");     System.Console.ReadLine();     return 0;   }  }}




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel