dcsimg
 

.NET Remoting Using a New IPC Channel

Tuesday Feb 22nd 2005 by SeenivasaRagavan Ramadurai

Learn a new way to handle .NET remoting.

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();

      }
   }
}
Home
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved