Using Interfaces In .NET Remoting

Creating the Client

To create the client program, go to File->New->Project and choose a C# Console Application named RemotingExampleClient.

IRemotingExampleService resService =(IRemotingExampleService)Activator.GetObject(
  typeof(IRemotingExampleService),
  "tcp://localhost:9988/RemotingExampleService");
Console.WriteLine("RESUME:\n"+ resService.GetFormattedResume());

Notice that we're using the IRemotingExampleService here instead of RemotingExampleService? That is because our client side code knows only about the interface, not the implementation the server is using. The result of this is the server is serving "RemotingExampleService", but our client is using "IRemotingExampleService".

IResume aResume = resService.GetResume();
Console.WriteLine("NAME:"+ aResume.GetFormattedName());
Console.WriteLine("RESUME:"+ aResume.GetFormattedResume());

This section of code makes use of the instance of IRemotingExampleService to return yet another interface. Here we don't have to make any calls to the Activator object because the .NET runtime accesses the server's Resume implementation transparently using the IResume interface.

To make this code work, we will need to add a referance to both our RemotingExampleInterfaces and System.Runtime.Remoting dlls as we did in step 2. Without this you will get compiler errors.

The complete code listing is below. For more information on the Activator object, see my article titled "Introduction to .NET Remoting" on this site.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using RemotingExampleInterfaces;

namespace RemotingExampleClient
{
  class RemotingExampleClient
  {
    static void Main(string[] args)
    {			
      ChannelServices.RegisterChannel(new TcpClientChannel());
      IRemotingExampleService resService =(IRemotingExampleService)Activator.GetObject(
        typeof(IRemotingExampleService),
        "tcp://localhost:9988/RemotingExampleService");
      Console.WriteLine("RESUME:\n"+ resService.GetFormattedResume());
      IResume aResume = resService.GetResume();
      Console.WriteLine("NAME:"+ aResume.GetFormattedName());
      Console.WriteLine("RESUME:"+ aResume.GetFormattedResume());
      Console.WriteLine("Press any key to continue...");
      Console.Read();
    }//END OF MAIN METHOD
  }//END OF RemotingExampleClient class
}//END OF RemotingExampleClient namespace

Compile this project and note the location of the .EXE file.

You might also like...

Comments

About the author

David Talbot United States

David Talbot is an experienced Software Architect with a diverse background including creating network applicances, working with television set top boxes, building Billing/CRM systems, Web Port...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match” - Bill Bryson