Minimal Remoting sample that works in the VS.NET IDE

This is a small sample app that actually works in the VS.NET IDE.

This is in three parts:

One common library to implement the remoting object,
One server and one client.

Common library (simple library project):

// SimpleRemoting.cs

using System;
using System.Runtime.Remoting;

// Add a reference to System.Runtime.Remoting
namespace SimpleRemotingLibrary
{
    /// <summary>
    /// This is a common object for the remoting
    /// </summary>
    public class RemoteHello  : MarshalByRefObject
    {
        static string msg = “Hello, Universe of .NET”;
        public string SayHello()
        {
            Console.WriteLine(RemoteHello.msg);
            return msg;
        }   
    }
}

// HelloServer.cs

using System;

// Add a reference to System.Runtime.Remoting
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
// Add a reference to SimpleRemotingLibrary
using SimpleRemotingLibrary;

public class RemoteServer : MarshalByRefObject
{
    [STAThread]
    public static void Main(string[] args)
    {
        TcpChannel channel = new TcpChannel(4000);
        ChannelServices.RegisterChannel(channel);
        RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteHello),
                                                            “HelloDotNet”,
                                                            WellKnownObjectMode.Singleton);
        System.Console.WriteLine(“Hit <enter> to exit…”);
        System.Console.ReadLine();
    }

}

// HelloClient.cs

using System;

// Add a reference to System.Runtime.Remoting
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
// Add a reference to SimpleRemotingLibrary
using SimpleRemotingLibrary;

class RemoteClient
{
    [STAThread]
    static void Main(string[] args)
    {
        try
        {
            TcpChannel channel = new TcpChannel();
            ChannelServices.RegisterChannel(channel);

            RemoteHello h = (RemoteHello) Activator.GetObject(typeof(RemoteHello),
                “tcp://127.0.0.1:4000/HelloDotNet”);
            System.Console.WriteLine( h.SayHello() );
        }
        catch(Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        System.Console.ReadLine();

    }
}

You need to start the server first…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s