Using WCF for Inter Process Communication

This post is intended as a simple-as-possible introduction to using the .net Windows Communication Foundation (WCF) in C# for Inter Process Communication.  I’m a fairly recent convert to .net and C# and have been very impressed at how simple and powerful this all was.

If you’re not familiar with WCF it is a real kitchen sink of a library that can accomplish anything from saving config files, web services with SOAP to IPC!

In this tutorial we are going to be creating 2 console apps – client & server – to communicate with each other using WCF. The console apps will need the System.ServiceModel Assembly added as a reference.

ServiceContract
The first thing you will need to define is the ‘ServiceContract’.  This is just the interface that we will be using over IPC. In our simple example we will be exposing 2 functions, Add & Subtract.

// ITestContract.cs
using System.ServiceModel;

namespace IPCTestServer
{
  [ServiceContract(Namespace = "https://gorillacoding.wordpress.com")]
  interface ITestContract
  {
    [OperationContract]
    int Add(int a, int b);

    [OperationContract]
    int Subtract(int a, int b);
  }
}

Server
Next you will need to provide an implementation of this interface on the server side:

// IPCTestServer.cs
using System.ServiceModel;

namespace IPCTestServer
{
  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
  public class IPCTestServer : ITestContract
  {
    public int Add(int a, int b)
    {
      return a + b;
    }

    public int Subtract(int a, int b)
    {
      return a - b;
    }
  }
}

We then need to create the ServiceHost object that will host this interface using the NetNamedPipeBinding. This is an in memory interface suitable for on-machine messaging.

//Program.cs
static void Main(string[] args) 
{
  string address = "net.pipe://localhost/gorillacoding/IPCTest";

  ServiceHost serviceHost = new ServiceHost(typeof(IPCTestServer));
  NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
  serviceHost.AddServiceEndpoint(typeof(ITestContract), binding, address);
  serviceHost.Open();

  Console.WriteLine("ServiceHost running. Press Return to Exit");
  Console.ReadLine();
}

Client

Finally, we will need to implement a client to actually call these functions using this interface. Here we will use the ChannelFactory to create an IClientChannel object that also implements our ITestContract interface.

// Program.cs
static void Main(string[] args)
{
  string address = "net.pipe://localhost/gorillacoding/IPCTest";

  NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
  EndpointAddress ep = new EndpointAddress(address);
  ITestContract channel = ChannelFactory<ITestContract>.CreateChannel(binding, ep);

  Console.WriteLine("Client Connected");

  Console.WriteLine(" 2 + 2 = {0}", channel.Add(2, 2) );
  Console.WriteLine(" 256 - 89 = {0}", channel.Subtract(256, 89));

  Console.ReadLine();
}

And that’s all there is to it!

D

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s