A Dutch software developer living in Chile
# Monday, June 08, 2009
Remoting or WCF?

.Net Remoting sounds old, and it is essentially old. Though this is a technique not commonly used and not so popular, it is still valid and it still works today. (Even in .Net 4.0) WCF has gained more popularity since .Net 3.0. Now WCF is popular and .Net Remoting is nearly forgotten. Still, I have to use .Net Remoting because some of my customers are still using – glump – Windows 2000. Guess what Windows 2000 supports? Only .Net 2.0.

.Net Remoting supports almost any host, including IIS. Just like WCF, you can create your own Windows Service to host your Remoting objects or host them in IIS. There are some considerations that you should take when using .Net Remoting, but once you’ve gotten used to it, you’ll see that it’s quite a easy way to establish inter-process communication.

So time for some examples… For this example we’ll use 3 assemblies. One class library and 2 console applications.

First, let’s create a remotable object in a class library:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyRemoteObjects
{
  public class RemotableObject : MarshalByRefObject
  {
    public RemotableObject()
    {
      startTime = DateTime.Now;
    }

    private DateTime startTime;

    public DateTime GetDateTime()
    {
      Console.WriteLine("Someone wants to know the time");
      return DateTime.Now;
    }

    ~RemotableObject()
    {
      Console.WriteLine("I'm shut down after {0}", DateTime.Now - startTime);
    }
  }
}

Next, here’s the source code for the server application:

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Collections.Generic;
using System.Text;
using MyRemoteObjects;

namespace MyRemoteService
{
  class Program
  {
    static void Main(string[] args)
    {
      HttpChannel channel = new HttpChannel(8080);
      ChannelServices.RegisterChannel(channel, false);

      RemotableObject object1 = new RemotableObject();

      Console.WriteLine("I'm the server");
      ObjRef ref1 = RemotingServices.Marshal(object1, "object1uri");
      Console.WriteLine("ObjRef.URI: {0}", ref1.URI);
      

      Console.WriteLine("Press enter to quit the app");
      Console.ReadLine();

      // tear down
      RemotingServices.Disconnect(object1);
      ChannelServices.UnregisterChannel(channel);

      // The ServiceClass object's Finalize method writes a message to
      //  the console. A single object will almost always succeed in 
      //  running its Finalize method before the Console is finalized;
      //  in a larger application, you could ensure that all objects 
      //  finalize before the application ends by calling the garbage 
      //  collector and waiting.

      GC.Collect();
      GC.WaitForPendingFinalizers();


    }
  }
}

Last, here’s the source for the client application:

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Collections.Generic;
using System.Text;
using MyRemoteObjects;

namespace RemotingExample
{
  class Program
  {
    static void Main(string[] args)
    {
      HttpChannel channel = new HttpChannel(0);
      ChannelServices.RegisterChannel(channel, false);

      RemotingConfiguration.RegisterWellKnownClientType(typeof(RemotableObject), "http://localhost:8080/object1uri");

      RemotableObject remObject = new RemotableObject();
      Console.WriteLine("I'm the client");
      try
      {
        Console.WriteLine("The time on the server is: {0}", remObject.GetDateTime());
      }
      catch (Exception ex)
      {
        Console.WriteLine("Oops: {0}", ex);
      }
      Console.ReadLine();
      

    }
  }
}

After building and running this, you’ll get something like this:

image

If you would put this into a service, you can actually talk with your Windows service without much of a hassle. Hope this helps! Objects that are passed from the server to the client need to be either serializable or inherited from MarshalByRefObject.


Monday, June 08, 2009 1:16:06 PM (Pacific SA Standard Time, UTC-04:00)  #    Comments [0]  

Comments are closed.