Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 39,825 times

Contents

Related Categories

Using Interfaces In .NET Remoting - Creating the Client

David Talbot

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.

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 Portals and more. He has also provided technical guidance in different capacities on two C# books. David is currently finishing up work on a real estate analytics application in C# for Pathfinder Technologies and seeking additional contract or permenet work.

Comments

  • Posted by PaulLeo on 24 May 2004

    Hi,

    I too have the same problem. Did you find any solution for that?

    Regards
    Paul

  • re:

    Posted by asafsn on 28 May 2003

    Thanks again but I'm sorry to say I'm still not even in the stage you described in your reply of using a method of the remote object. I'm getting this exception in the following line:

    -------------...

  • Posted by James Crowley on 28 May 2003

    You may well find that is due to the use of the "Optional" keyword in VB.NET with a default value, rather than overloading the methods. Default values are not supported in C#. (... there are a few cav...

  • Re: C# and VB remoting

    Posted by asafsn on 28 May 2003

    thanks for the reply.

    Its time for me to be more specific.

    I wrote a server client application in C# using remoting
    and it worked. Then I replaced the client with another
    clients written in...

  • Posted by James Crowley on 28 May 2003

    I can certainly say that it is possible - the great thing with .NET is its ability to not care what .NET language you write any components in.... but I haven't got any examples, I'm afraid.