Interprocess Communication via Self Hosted WCF

Lately I was asked if there is a way to allow 2 apps to communicate even if they are belonging to 2 different exe’s . Fortunately for me I have the source code of both 2 apps. The main reason why instead of merging the projects, we went to IPC or interprocess communication is because, I belong to a team that maintains the other windows forms app and the other project or exe belongs to another team that has their own deliverables. And another thing is, we want to deploy the IPC functionality the soonest. It means it is badly needed.

The way to do it is have a listener in the second app and let the first app talk to the second app with the same url.

Oh, first lets create something (object) that can be shared between the 2 apps. When you communicate, you transfer knowledge, words, etc. In our case lets transfer some data to the other app.

The Data

[DataContract]
[Serializable()]
public class UserInformation
{
[DataMember]
public string Name { get; set; }

[DataMember]
public DateTime BirthDate { get; set; }

[DataMember]
public string Address { get; set; }
}

The Service Contract

This will be self hosting service which will be hosted in the listener app.

[ServiceContract(Namespace = “https://www.francorobles.com”)]
public interface IUserServiceContract
{
[OperationContract]
int UpdateUserInformation(UserInformation userInformation);

}

The Service Implementation

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class UserService : IUserServiceContract
{
public int UpdateUserInformation(UserInformation userInformation)
{
return 1;
}
}

Starting the listener

This code block will be in the listener app. What it does is we instantiated a wcf service inside our windows app via named pipe.

Dim address As String = String.Concat(“net.pipe://localhost/user/userservice”, Environment.UserName)
Dim _userService As UserService = New UserService()
Dim serviceHost As New ServiceHost(_userService e)
Dim binding As New NetNamedPipeBinding(NetNamedPipeSecurityMode.None)
serviceHost.AddServiceEndpoint(GetType(IUserServiceContract), binding, address)
serviceHost.Open()

Calling the listener

Here we called the listener via the same url that was instantiated in the listener app. Then we created a UserInformation object and passed it like we do in a normal wcf service.

Dim address As String = String.Concat(“net.pipe://localhost/user/userservice”, Environment.UserName)
Dim binding As New NetNamedPipeBinding(NetNamedPipeSecurityMode.None)
Dim ep As New EndpointAddress(address)
Dim channel As IUserServiceContract = ChannelFactory(Of IUserServiceContract).CreateChannel(binding, ep)

Dim _userInformation as new UserInformation()
_userInformation.Name = “Franco Robles”
_userInformation.BirthDate = DateTime.Parse(“1985-10-08”)
_userInformation.Address = “Makati, Philippines”
channel.UpdateInformation(_userInformation)

 

And thats pretty much about it. There are many types of IPC (Inter Process Communication), WCF via named pipe just suits my needs. Maybe you encounter the same scenario sooner or later and hopefully this post will be a starting point for you.

Happy Coding!!!

 

 

 

 

 

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

Blog at WordPress.com.

Up ↑

%d bloggers like this: