Second step in remoting example.

This is an extension of my earlier example.
This version makes the remote object methods virtual.
This means that the client does not actually need to know anything about the actual
type of the implement object.

Common library:
// 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 virtual string SayHello()
        {
            Console.WriteLine(RemoteHello.msg);
            return msg;
        }   
    }
}

// Server
// 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;

namespace SimpleServer
{

    public class RemoteServer : MarshalByRefObject
    {
        [STAThread]
        public static void Main(string[] args)
        {
            TcpChannel channel = new TcpChannel(4000);
            ChannelServices.RegisterChannel(channel);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloDescendant),
                “HelloDotNet”,
                WellKnownObjectMode.Singleton);
            System.Console.WriteLine(“Hit <enter> to exit…”);
            System.Console.ReadLine();
        }
    }
    /// <summary>
    /// This is one technique where you can distribute a common base class
    /// and only implement the details in the server object.
    /// </summary>
    class HelloDescendant : RemoteHello
    {
        private static string msg = “This is a descendant”;
        public override string SayHello()
        {
            Console.WriteLine(HelloDescendant.msg);
            return msg;
        }   
    }

}

// Client – this is unchanged
//  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
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [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();

    }
}

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…