.NET Remoting Using a New IPC Channel

This article is based on .NET Framework 2.0 beta 1. Remoting in .NET Framework 2.0 uses some of the new & cool features such as am IPC channel that enables an on-box communication (in other words, communication between server app and client app when both reside on the same box). This channel won't use any network layer when the client and server communicate. The IPC channel is exactly like the TCP and HTTP channels in terms of the schema; the schema that will be IPC: // and name of the IPC channel that you've specified and then the name of the remote object behind it. The complete URI looks something like this: IPC: //NameoftheIPC/objectUri.

The IPC channel doesn't use ports like the TCP and HTTP channels; it is based on Named Pipes. It uses a simple scheme to specify a location that is similar to TCP or HTTP and looks like what was just mentioned above.

.NET Framework 2.0 out of the box supports three channels—TCP, HTTP, and IPC—and two formatters, Binary and SOAP. In this article, I will show how you can do .NET Remtoing by using the IPC channel.

.NET Remoting can be called as Web services of any form or anywhere, because by using .NET Remoting you can host Web services in any one of the following forms:

  • Just as a Console Application
  • Windows Application
  • Windows Service
  • IIS (using a HTTP channel and Binary Formatter)

The remoting application should have three pieces:

  • Remote able objects, which I call Shared Assembly (SA), because both the server and client application reference these objects. This SA should be derived from MarshalByRefobj, which makes this object Remote able. Remote able objects should have only interface definitions; that way, you hide the implementation from the client application.
  • Server Application that hosts the Remote able object.
  • Client Application that makes the call to the server.

Let me show you how you can build these three applications using .NET Framework 2.0.

Firstm create a Shared assembly. This is nothing but a .NET Class Library or simply a .DLL. This will be your remote able object. The shared assembly should be having only interface method definitions that will be called remotely. This is the best practice to create a shared assembly. If you follow this approach, whenever an interface method implementation changes, the client application need not reference a new version of the assembly. You need to give the new assembly only when you change the interface definition.

Shared Assembly

namespace Remoteable
{
   public interface  ISharedAssemblyInterface
   {
      int Addition(int a, int b);
      int Multipliation(int a, int b);

   }
}

Next, implement the Shared Assemlby interface. The class that implements this interface is called MyRemoteObject; it is derived from both MarshalByRefObject and the shared assembly ISharedAssemblyInterface. Here, the important thing is that every remote able object should be derived from MarshalByRefObject. This class resides in the System namespace.

Shared Assembly Implementation

namespace IPCChannelRemoting
{
   public class MyRemoteObject:MarshalByRefObject,
                ISharedAssemblyInterface
   {
      public  MyRemoteObject()
      {

      }

      public int Addition(int a, int b)
      {
         return a + b;
      }

      public int Multipliation(int a, int b)
      {
         return a * b;
      }


   }
}

Server Application that Hosts the Remote Able Objects

In the server application, you will use the IPC channel as the remoting channel. To use this, you need to import the following namespace:

using Remoteable;    //
using IPCChannelRemoting;
using System.Runtime.Remoting.Channels.Ipc ;    //Importing IPC
                                                //channel
using System.Runtime.Remoting.Channels ;
using System.Runtime.Remoting;


namespace MyServer
{
   class Program
   {
      static void Main(string[] args)
      {
         //IPC port name
         IpcChannel ipcCh = new IpcChannel("IPChannelName");

         ChannelServices.RegisterChannel(ipcCh);
         RemotingConfiguration.RegisterWellKnownServiceType
            (typeof(IPCChannelRemoting.MyRemoteObject),
                    "SreeniRemoteObj",
                    WellKnownObjectMode.SingleCall);
         Console.WriteLine("Please enter to stop the server");
         Console.ReadLine();
      }
   }
}

The client application will call the method on the Remote objects. It is hosted by the server application

namespace MYClient
{
   class Program
   {
      static void Main(string[] args)
      {
         IpcChannel ipcCh = new IpcChannel("myClient");
         ChannelServices.RegisterChannel(ipcCh);

         ISharedAssemblyInterface obj =
            (Remoteable.ISharedAssemblyInterface)Activator.GetObject
            (typeof(Remoteable.ISharedAssemblyInterface),
             "ipc://IPChannelName/SreeniRemoteObj");
         Console.WriteLine(obj.Addition(10, 10));
         Console.ReadLine();

      }
   }
}


About the Author

SeenivasaRagavan Ramadurai

Seenivasaragavan Ramadurai is a .NET consultant, He has been working with .NET technology since pre beta releases. Seenivasa background includes Master's in Computer Science and B.Sc. Mathematics. He has over 9 years of software development experience with Microsoft technologies and has extensive experience developing client-server, distributed, Web services, and component based applications using Visual Studio .NET. Before moving to .NET, Seenivasa has worked on MFC, COM, ATL, and Visual C++ based applications. If you are looking for a consulting help, contact him at skbbaba23@gmail.com

Comments

  • How about sharing a class between client and server

    Posted by kairen on 12/21/2014 08:31pm

    Is it possible if sharing complex data like a class or a callback between client and server ? For example: I put a function to ISharedAssemblyInterface: public class MyData { public int a,b; } public interface ISharedAssemblyInterface { int Addition(int a, int b); int Multipliation(int a, int b); void DoThing(Mydata data); } Then implement DoThing(...) in MyRemoteObject: void DoThing(Mydata data) { data.a = 1; data.b = 2; } So , is it possible from client to use this function ? I had tried this way, but my client did crash.

    Reply
  • Test

    Posted by rameshsoni on 08/03/2006 06:19am

    Google

    ramesh.libra@gmail.com | Personalized Home | My Account | Sign out
    Google

    Web    Images    Groups    News    Froogle    Maps    more »
     
      Advanced Search
      Preferences
      Language Tools


    Advertising Programs - Business Solutions - About Google - Go to Google India

    ©2006 Google

    Reply
  • Testing google search

    Posted by rameshsoni on 08/03/2006 06:18am

    
    
    Google

    Reply
  • Testing

    Posted by rameshsoni on 08/03/2006 06:15am

    Test done for bold chars.
    Test done for italic chars.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date