Vasteras - Sweden, Belgrade, Nis - Serbia

March 11, 2010

Troxo - Software Development Company

On .NET Remoting

August 13th, 2007

Recently we had a .NET based project where we had to enable simple client to server communication. We needed a fast solution, totally embedded into applications (meaning – no IIS web services were allowed, unfortunately).

Basically, the challenge was: the main application has to provide remote access to its dynamically created objects, like in the image bellow:

Remoting Project Challenge

We found some really good articles on the net like Remoting in C# and Remoting and Distributed Computing in C#, but none explaining everything we needed – so after some digging into the MSDN, here is the solution.

"Single call" remoting creates a new remoting object for every call by the client. "Singleton" reuses the same object for every client’s call.

"Single call" remoting was not fitting our problem because we needed just one proxy object which will forward calls to internal objects.

In our project there was a small catch with the use of "Singleton". Basically, if you do not create an object before the first call from the client, the object will be created automatically (with the first client’s call), leaving it unbound to our server objects (in the image above that would mean that "remoting facade" does not have references to internal objects – Object 1, Object 2, etc.).

The solution is to create a remoting object, initialize it with references to local objects and use RemotingServices.Marshal method to bind the remoting object instance with the remoting service (like in the following example from the attached project):

Logics.Counter counter = new Logics.Counter();
Logics.RemoteCounter remotingCounter = new Logics.RemoteCounter();
//initialize remoting object with the reference to local object
remotingCounter.SetLocalCounter(counter);
//select channel to communicate
TcpChannel channel = new TcpChannel(8085);
ChannelServices.RegisterChannel(channel, false); //register channel
//register remoting service
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Logics.RemoteCounter), "RemoteCounter", WellKnownObjectMode.Singleton);
//bind remoting object with remoting service
RemotingServices.Marshal(remotingCounter, "RemoteCounter");

The complete sample: troxosamplesremoting.zip