Library tutorials & articles

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.

Comments

  1. 24 May 2004 at 03:42

    Hi,


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


    Regards
    Paul

  2. 28 May 2003 at 06:58
    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:

    -----------------------------------VB Code------------------------------------------------------------
    Sub Main()

           ChannelServices.RegisterChannel(New HttpChannel())
           Dim factory As RemotingClientsInterfaces.IAccountantExamFactory   'this is the object Im tring to remote

           'this is the line where the excaption accure
           factory = CType(Activator.GetObject( _
                         Type.GetType("RemotingClientsInterfaces.IAccountantExamFactory"), _
                          "http://10.0.0.172:8080/IExamFactory"), RemotingClientsInterfaces.IAccountantExamFactory)

          .
          .
          .
    ---------------------------end of Code-------------------------------------------------------------------

    See anything suspicious ???


  3. 28 May 2003 at 06:44
    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 caveats when using different languages). So basically, when calling the VB.NET methods, ensure you provide all parameters including the "Optional" ones.
  4. 28 May 2003 at 06:38
    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 VB.NET and got an exception while trying to create the remote object:

    "An unhandled exception of
    type 'System.ArgumentNullException' occurred in
    mscorlib.dll
    Additional information: Value cannot be null."

    do you have any suggestions what so ever ???
  5. 28 May 2003 at 05:35
    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.
  6. 28 May 2003 at 05:14
    Can I use remoting when the server is written in C# and the client in VB.NET ? If so does any one knows where I can find an example for this?

    My E-Mail: assaf@nipendo.com

    Thanks
  7. 12 Nov 2002 at 16:57
    Well the article describes very well how to use .NET Remoting in your application.But does not give a good comparision with widely used remoting procedure i,e DCOM.
    Unless a developer get the comparision between two technologies he/she may not choose the new one.

    Ghanshyam.
  8. 08 Nov 2002 at 16:40
    Hello, I would like to remote an interface to a client-activated object.  This sample, and others I've seen assume a singleton/single-use object, which won't work for my application.  Or, is there no way to do CAO's with interfaces?  Help!  Thank you.
  9. 04 Sep 2002 at 04:31

    Hi,


    Can you please pass me the Client application source code for your article .Net Remoting. I think you forgot to upload in your site at http://www.developerfusion.com/show/2082/3/


    Can you please forward the source code to my e-mail ID @ r_nazermd@yahoo.com


    Thanks and Regards
    Nazer Mohamed

  10. 25 Jun 2002 at 00:44

    Good article.  The client side code was missing until I went to the printer friendly version.  I ran the code and it worked as expected.  


    I have a couple of questions about interfaces.  Is it possible to initialize a type "such as byte [] array" within the main program and allow the interfaces to access that initialized type so that the clients could do something with it?  I thought that the use of interfaces would be a good tool for clients to use to get information from a server that is constantly polling external data.  The only examples that I have seen so far, show me that the main thread is completely separate from the interfaces.  Is this true?


    Thanks,


    Tom McDaniel

  11. 01 Jan 1999 at 00:00

    This thread is for discussions of Using Interfaces In .NET Remoting.

Leave a comment

Sign in or Join us (it's free).

David Talbot 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 Portal...
AddThis

Related podcasts

  • More jQuery in ASP.NET

    In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, and Scott Koon conclude their discussion of Microsoft's jQuery in ASP.NET announcement1.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are ...

Events coming up

  • Nov 18

    15 Minutes of Fame

    Dresher, United States

    This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.

Want to stay in touch with what's going on? Follow us on twitter!