.NET Remoting : How to 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 MyObject 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 MyRemoting
{
   /// Sample object to demonstrate the use of .NET Remoting.

public class MyObject : MarshalByRefObject
{
   public SampleObject()    
   {    
   }
   /// Return a hello message

   public string HelloWorld()    
   {      
       return "Hello World!";    
   }
}
}

No comments:

Post a Comment