views:

37

answers:

3

Hello,

I am currently working on 2 .NET apps which must communicate with each other. Simple Windows Messaging was chosen and this works nicely at the moment. The data that is sent in the messages is a simple string, but now I have created a message class which contains a command (enum) and a data (string) member, and later possible other members.

When I send an instance of such an message class, it is serialized to bytes and then converted to a base64 string. This is then sent out using Windows' SendMessage(). On the other side I do the opposite. Eventually, the original object is restored and available in the other app.

While this mechanism seems to work, I was wondering if this is safe to do. Indeed, there is some overhead, base64 string is much longer than the original string solution (but I would have to parse this tring manually to get command and data part) Is there a maximum size for messages that can be sent with SendMessage ?

Also I'd rather stay away from .NET remoting for this project and stay with the SendMessage solution.

Any idea's ? Maybe use JSON instead to limit the overhead ?

Thanks.

Pika

A: 

If you just need to send messages look at a message bus architecture like Rhino Service Bus or NServiceBus. They will provide a relatively simple and much more robust messaging implementation.

David Lynch
Interesting, but for now I only need IPC on the same machine.
pika81
A message bus system will work between two processes on the same machine also. And if you need to work between two machines later the change is a "no brainer".
David Lynch
A: 

I'd really recommend using a more modern, scalable approach. For example, you could easily use named pipes to communicate, and just use standard stream functionality instead of trying to send messages that need to be encoded.

(Personally, I'd probably use WCF, as that has so many benefits, including allowing this to work across different machines for free...)

Reed Copsey
True, the problem is I have to stick with the .NET 2.0 Framework. I may look into named pipes in the future however.
pika81
@pika81: It sounds like you've found a solution, but if not, you can consider just using something that provides named pipe support for .net 2, like: http://www.switchonthecode.com/tutorials/interprocess-communication-using-named-pipes-in-csharp
Reed Copsey
Thx, I also found this very usefull post: http://blog.benday.com/archive/2008/06/22/23182.aspx
pika81
@pika81: That's good if you're .NET 3.5 -
Reed Copsey
A: 

Use XDMessaging (http://xdmessaging.codeplex.com/) which supports WM_COPYDATA, Named pipes and IOStream messaging- providing a highly flexible, robust and tested solution, instead of rolling something yourself.

How about using Binary Serialization on your class (http://msdn.microsoft.com/en-us/library/4abbf6k0(v=VS.71).aspx) with XDMessaging? It would be quite compact and very easy to implement.

Keith Blows
Under the hood I'm already using XDMessaging. It works quite well indeed, but you can only send strings with it. Using [Serializable] and BinaryFormatter gives me a byte array that is longer than the method I was using, but they both work.
pika81
As it seems, the aproach I was using resulted in objects that did not correctly deserialize. I then used the method you described and this works correctly. Thanks!
pika81