Introduction
I decided to post this article because I never found what I was looking for on the Web. What I needed was an application that could "understand" what another application was saying. I'd found tons of examples of remoting and the simple Hello World! example, and of course the one where you can see the creation of an instance on the server for one of the object's attributes on the client. But that was not what I needed. I wanted to create an object, set its attributes and send it over the network to a different application which was programmed to do something with that object.
As Muhammad Musa Ali suggested, I'll explain a little bit more about remoting in case you have no clue what it is good for. Let's say you're building an IM app. You have to communicate with others through the network (for a start, let's just talk about a LAN). With the .NET technologies, you could open a TcpClient
connection and use a NetworkStream
in order to send string
s over the network. You could "create" your own SOAP protocol in order to send more than one information to the other client as to who is sending the message, at what time the message was sent and so on. But if you could send an object of your domain, what if you could have a Message
class with attributes as ID
, text
, time
, and client
, where client
is an instance of a class also from your domain with attributes such as name
, IP
, address
, state
, etc. Well, that's what remoting is for.
Background
I believe that if you're reading this, you are familiar with remoting and serialization, and you want an example of it. If that's not your case, I'd recommend you to read a little bit about them and then run the code.
Using the Code
This solution is really simple, it has three projects in it (Client, DLL and Server). The DLL project has an interface (ILogic
) and a class (Client
). This Client
class is the one I will create on one application, set its attributes and send it over the network to a different app (Server
). The ILogic
is an interface implemented by the server in the Logic
class. The three methods exposed by the interface are the ones the client
app sees. The Client
app has a simple form from which you execute methods from the server and one of them has a client
object as a parameter.
So, to get it running, just right click on the Server
project and under the Debug item click on Start new instance. After that, do the same as many times as you wish with the client project. When the client form appears, click the "Mensaje" button and take a look at the console. After that, just play around with the other buttons and messages.
Take a look at the "Hora" button and what you see on the console. Compare the two, amazing isn't it?
Bottom Line
It is really easy to use remoting and if you are in a controlled and closed environment, remoting is the right tool for you. But keep in mind that if you need to interact with some application outside your LAN, Web services would be the right tool for you. As you may have noticed, Web services are just a particular implementation of remoting.
History
- 14th March, 2006: Initial post
Sebastian Gomez is a System Engineer in Uruguay, South America.
He's been working with .net technologies since beta 1.
You can reach Sebastian at: SebastianGomezCorrea[at]gmail.com