views:

354

answers:

6

Is there a simple way to send objects between applications using C#? The objects will not always be the same, there would be (for example) a "Request" and "Response" class and each side of the connection would have a sort of switch statement which decides which object has been received.

Is there a simple way to achieve something like this in C# without the need for 3'rd part libraries?

+2  A: 

.NET remoting does more or less what you want, and you can use your interface classes as the classes which do the "switch statement"

http://msdn.microsoft.com/en-us/library/kwdt6w2k%28VS.71%29.aspx

Tim
This does work, but you will incur a large(r) overhead in space and network traffic.
monksy
.net remoting is a predecessor to WCF. Seriously consider using WCF while considering .net remoting. The value of higher level transporting, the value of the ease for de/serializing objects, and IDE support outways the overhead.
Russell
(Reference for previous comment): http://msdn.microsoft.com/en-us/library/72x4h507%28VS.85%29.aspx
Russell
@steven - Serializing to XML would create a far larger payload, with just as coarsely grained messaging. .NET remoting, from what I remember (can) use(s) byte streams for sending over the serialized form of an object, and therefore has a smaller payload.@Russell - agreed, I was going to mention about WCF, but the OP seemed to imply they wanted something a bit more "custom". The use of WCF is very elegant too.
Tim
+3  A: 

The easiest way to accomplish over a raw TCP connection this is to create a DLL which is referenced by the applications on both sides of the connection. Put all of your communication classes in this DLL and mark every type as Serializable. This enables the objects for binary serialization and hence an easy conversion into a byte[] which can be passed directly through the TCP connection.

If you have a bit more flexbility you may want to consider something a bit higher level though like Remoting Services or WCF

JaredPar
Serialization via Tcp stream would be my first choice over Remoting. And WCF can be way more intense than what this operation requires. Depends on the overall usage, requirements, and/or expectations.
dboarman
+2  A: 

If you do not need to send the full binary object (with logic instructions) you can serialize the request and response classes to XML and send over the text via streams.

monksy
+3  A: 

WCF could work here, you just need to set up an interface describing the objects you are going to pass. Here is a good tutorial on using WCF for interprocess communication.

tbischel
This solution really is quick and easy to do.
Russell
+1  A: 

The definition of "simple" also depends on your requirements. Built-in functionality (eg. WCF) would not be classified as 3rd party libraries and therefore provide a simple way to securely send objects between C# applications. WCF can automatically generate proxies for you so all you have to do is define your data contracts and expose your service. You can add a service reference inside visual studio and the methods are exposed.

I will re-iterate, it really depends on what you want to do, to determine what the simplest solution is.

Russell
A: 

I'd always try to look at the problem as sending data between applications, not objects. Distributed object models almost always end in tears, as you don't really have shared state.

kyoryu