jamin on February 19th, 2003

Well it’s not WebServices, but I was playing around with remotinglast night and found it works quite well in mono, at least for mysimple test. I started to look at remoting after a discussion withAdam and another friend online about how to expose an API of an existingprocess to external processes. The example Adam was thinking of was,suppose you have an instant messenger app and you want a certain api to be exposed to external procs. So you want to be able to write anotherprogram or script in some .NET language and be able to do stuff like change your “away status” or maybe get queued messages or something.

Remoting seems to be the answer. Click below for my test code.

In this simple example you have the SimpleObject.dll built from its .cs file whichrepresents the API we’re going to expose. You also have SimpleServer.exebuilt from its .cs file which is our application. And you haveSimpleClient.exe which is our external client that wants to control theapplication “remotely”. In this simple example all that occurs is anobject is created which has a counter. Every time the GetCount() method iscalled on the object the counter is incremented and the current count isreturned. So to test you start the SimpleServer.exe and just leave itrunning. Then you run SimpleClient.exe as many times as you want. Everytime you run it the count increases by one. You can even changeSimpleClient to run on a different machine. :)

SampleObject.cs:


using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace TestCode.Remoting
{
  public class SampleObject : MarshalByRefObject
  {
    private int counter;
    public SampleObject()
    {
      counter = 0;
    }
    public int GetCount()
    {
      counter++;
      return counter;
    }
  }
}

SampleServer.cs:


using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace TestCode.Remoting
{
  public class SampleServer
  {
    public static void 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 GetCount
      // Every incoming message is serviced by the same object instance.
      RemotingConfiguration.RegisterWellKnownServiceType(
        typeof(SampleObject),
        "GetCount",
        WellKnownObjectMode.Singleton );
      System.Console.WriteLine("Press the enter key to exit...");
      System.Console.ReadLine();
    }
  }
}

SampleClient.cs:


using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace TestCode.Remoting
{
  public class SampleClient
  {
    public static void Main(string [] args)
    {
      // Create a channel for communicating w/ 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
      SampleObject obj = (SampleObject) Activator.GetObject(
        typeof(TestCode.Remoting.SampleObject),
        "tcp://localhost:8080/GetCount" );
      // Use the object
      if( obj.Equals(null) )
      {
        System.Console.WriteLine("Error: unable to locate server");
      }
      else
      {
        Console.WriteLine("counter: {0}", obj.GetCount());
      }
    }
  }
}

Makefile:


CSC=mcs
%.exe: %.cs
	$(CSC) /r:System.Runtime.Remoting /r:SampleObject.dll $<
%.dll: %.cs
	$(CSC) /r:System.Runtime.Remoting /target:library $<
all: SampleObject.dll SampleServer.exe SampleClient.exe
SampleObject.dll: SampleObject.cs
SampleServer.exe: SampleServer.cs SampleObject.dll
SampleClient.exe: SampleClient.cs SampleObject.dll
clean:
	rm -f *.dll *.exe

Tags:

2 Responses to “.NET Remoting with Mono”

  1. s/Simple/Sample/g. Words are confusing. :) Another note: This code will only work with CVS currently, or Mono 0.20 once it’s released.

  2. hmm Intresting..currently i am working on a project which have a .NET remoting module .I am intrested to know that,on Linux platform,is Mono capable enough to configure different applications remotely like Apache,running remote pricess