Improved .NET Remoting, Part 1: Same-Box Communication

.NET remoting enables application communication. It is a generic system that different applications can use to communicate with one another. By exposing its objects to remote processes, .NET allows interprocess communication. The applications can be located on the same computer, different computers on the same network, or even computers across separate networks. Versions 1.0 and 1.1 of the .NET Framework provided TcpChannel and HttpChannel for communication. These channels required network communication regardless of where the applications were actually located relative to each other.

The Windows operating system features an interprocess communication system (commonly referred to as IPC). This communication method is designed for applications on the same computer to communicate and does not use network communication. Because IPC does not use network communication, the IPC channel is significantly faster than the TCP and HTTP channels for communication between app domains on the same computer. The System.Runtime.Remoting.Channels.Ipc is a new namespace in the pending release of the .NET Framework 2.0 that contains classes designed to use the IPC channel.

The remainder of this article covers the process of remoting and walks through the following example.

Creating a remotable object

Creating a server to expose the remotable object

Creating a client to connect to the server and consume the object

All you need to follow along is a familiarity with remoting, which you can acquire by referring to a prior article on the topic.

Create a Remotable Object

A remotable object is an object that inherits from MarshalByRefObject. The following sample demonstrates a simple class to expose the omnipresent "hello world". This object exposes a single method (HelloWorld) that will return a string. Anything that is serializable can be returned from the method call.

Create a new C# class library project. Add a class called SampleObject and put in the following code:

Create a Server to Expose the Remotable Object via IPC

You need to create a server object that will act as a listener to accept remote object requests. If you were using the TCP or HTTP channels, you'd have to register the listener on a specific port number, but not so for IPC. You create an instance of the channel and then register it for use by clients through the ChannelServices, and then give it a name. The service can be registered as WellKnownObjectMode.SingleCall, which results in a new instance of the object for each client, or as WellKnownObjectMode.Singleton, which results in one instance of the object used for all clients.

Create a new C# console application project. Add a class called SampleRemotingServer and paste in the following code:

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
namespace CodeGuru.RemotingSample
{
/// <remarks>
/// Sample server to demonstrate the use of .NET remoting and IPC.
/// </remarks>
public class SampleRemotingServer
{
public static void Main()
{
// Create an instance of a channel
IpcServerChannel serverChannel = new IpcServerChannel("hello");
ChannelServices.RegisterChannel(serverChannel);
// Register as an available service with the name HelloWorld
RemotingConfiguration.RegisterWellKnownServiceType( \
typeof(SampleObject),
"HelloWorld",
WellKnownObjectMode.Singleton );
Console.WriteLine("Listening on {0}",
serverChannel.GetChannelUri());
Console.WriteLine("Press the enter key to exit...");
Console.ReadLine();
}
}
}

Add a reference to System.Runtime.Remoting in the project; otherwise, the IpcChannel and other related classes will not be found. In addition, add a reference to the project containing the SampleObject. Otherwise, the code will not compile because it won't know how to find a reference to SampleObject.

Improved .NET Remoting, Part 1: Same-Box Communication

Create an IPC Client to Use the Remotable Object

Now that you have your remotable object and a server object to listen for requests, create a client to consume it. The client is very simple. It will connect to the server, create an instance of the object from the server, and then execute the HelloWorld method.

Create a new C# console application project. Add a class called SampleRemotingClient and paste in the following code:

Add a reference to System.Runtime.Remoting in the project; otherwise, the IpcChannel and other related classes will not be found. In addition, add a reference to the project containing the SampleObject. Otherwise, the code will not compile because it won't know how to find a reference to SampleObject. Compile the class to make sure you have everything correct.

Alternately, the code for creating an instance of the remote object could also look like the code below:

Both accomplish the same thing, but the second version is a little cleaner in my opinion. It uses the RemotingConfiguration class to register the SampleObject to come from the remote location rather than using the Activator.

Putting It All Together to Test the IPC Remoting Sample

Once you have created the projects and successfully compiled each of them, you are ready to try it out. Start the server by running the compiled executable. After the server successfully starts, it will result in a console window being displayed with the message "Press the enter key to exit..." (see Figure 1).

[RemotingServer.jpg]

Figure 1. Remoting Server Console Window

The server is listening, so you are now ready to run the client. Executing the client, whether by running the executable or debugging in Visual Studio should result in "Hello World!" being displayed in a separate console window (see Figure 2). The window will pause until you press the Enter key to allow you time to see the resulting output message.

[RemotingClient.jpg]

Figure 2. Remoting Client Console Window

Straightforward Same-Box Communication

.NET remoting is a powerful way to enable interprocess communication. The addition of the IPC channel and System.Runtime.Remoting.Channels.Ipc namespace adds a well performing option for communicating across application domains located on the same computer. The code is similar to the TCP and HTTP channels, so it should be relatively straightforward to replace them with IPC for same-box communication.

Future Columns

The topic of the next column is yet to be determined. If you have something in particular that you would like to see explained here, you could reach me at mstrawmyer@crowechizek.com.

About the Author

Mark Strawmyer

Mark Strawmyer is a Senior Architect of .NET applications for large and mid-size organizations. He specializes in architecture, design and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the fifth year in a row. You can reach Mark at mark.strawmyer@crowehorwath.com.

Comments

There are no comments yet. Be the first to comment!

You must have javascript enabled in order to post comments.

Leave a Comment

Your email address will not be published. All fields are required.

Name

Email

Title

Comment

Top White Papers and Webcasts

When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.

U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.