What is Remoting ?
The .NET Remoting system allows clients to call methods on objects across remoting boundaries. Remoting enables the communication between the client and the server by creating proxy objects.
The Client and the Server communicate by the following steps.
When a client object requests an instance of the server object, the remoting system at the client side creates a proxy of the server object. The proxy object behaves just like the remote object; this leaves the client with the impression that the server object is in the client's process.
When the client object calls a method on the server object, the proxy passes the call information to the remoting system on the client. This remoting system, in turn, sends the call over the channel to the remoting system on the server.
The remoting system on the server receives the call information and, on the basis of this information, invokes the method on the actual object on the server (creating the object, if necessary).
The remoting system on the server collects the result of the method invocation and passes it through the channel to the remoting system on the client.
The remoting system at the client receives the server's response and returns the results to the client object through the proxy.
Object Marshalling Specifies how a remote object is exposed to the Client Application. .NET Remoting offers us Object Marshalling by two ways
Marshal by Value ( MBV )
These objects are copied from the Server Application Domain to the Client Application Domain
Marshal by Reference ( MBR )
Clients just holds the reference to the Object and Proxy id used to access the Server
Marshal by Value
MBV should implements ISerialize interface or it should be marked with the SerializableAttribute so the the remoting System can serialize the objects
When a Client Calls a method by MBV, MBV object is Serialized, transferred to the client machine. It will be as a exact copy of the Server Side Object. Now the object is available in the client side so we can make calls to any methods.
Since the object is copied to the client machine, it will reduce to the roundtrips to the Server. So the execution will be faster
public class MarshalByValueObject
Marshal by Reference ( MBR )
MBR objects are remotable objects that must extend the System.MarshalByRefObject . They always reside on Server. When a client creates an instance of MBR object, a proxy object is created on the Client Side which holds the reference of the Server Side object.
public class MarshalByReferenceObject: MarshalByRefObject
We go for MBR when the object is size is large or when the functionality is available only in the Server.
Channels are used to transfer the details of the method call between Client and the Server.
The .NET Framework provides the provides the System.Runtime.Remoting.Channels namespace used to work with the channels. All channels implement the Ichannel interface.
The Channel Object on the receiving end will listen for the messages from the Client using the particular channel and portnumber. The Channel object on the sending End will send the information to the receiving end using the same protocol and port number.
.NET Framework provides two type of Protocols for sending and receiving the Object.
Implementations of the HTTP Channel is provided through the classes of System.Runtime.Remoting.Channels.Http
Implementations of the TCP Channel is provided through the classes of System.Runtime.Remoting.Channels.Tcp
If we want to define our own channel, we can do that implementing the IChannelReceiver and IChannelSender interfaces.
Formatters are the objects that are used to encode, decode, serialize and deserialze the objects when they are passed to the over the network.
.NET Framework provides to common Formatters
Soap Formatter is implemented in the SoapFormatter class which can be found at System.Runtime.Serialization.Formatters.Soap
Binary Formatter is implemented in the BinaryFormatter class which can be found at System.Runtime.Serialization.Formatters.Binary
If you want to use the different formatter, you can do that by implementing the IFormatter interface.
Remote Object Activation
As we saw earlier, only MBR needs the activation of the objects remotely. ( In MBV, the object itself is copied to the client but not in the case of MBR )
There are two types of Activation of objects available in Remoting
·Server Activated Objects ( SAO )
In server Activation, the objects are created when you firsta call a method from the client machine. The objects are not created in when you use the new keyword to create an instance
Client Activated Objects ( CAO )
But in case of Client Activated Objects, the objects are created in the Server when you use the new keyword for creating the instance.
In Server Activated Objects, there are two types activation
Single Call Activation Mode
For each and every client, an object will be created . When the client finshes the requests, then the object will be deleted from the memory and .NET Framework regains its space.
Singleton Activation Mode
In Singleton Activation, regardless how many clients, there will be only one object in the Server. It will serve for all the clients
Lifetime lease is the duration for which the Object in the Server should reside in the memory. Both SAO and CAO have their lifetime of their own.
The lifetime lease works as follows.
Whenever the object is created, its lifetime lease ( CurrentLeaseTime ) is set using InitialLeaseTime property. By default the leasetime is 5 minutes.
When the object the receives a call, the lifetime will be increased by 2 minuts by default. We can set the renewal time too by coding using the Renew() method.
When the leasetime reaches 0, then the object is marked for the garbage collection and it will be removed from the memory when Sponsol cannot renew leasetime.
In this Part 1 of remoting you learned about what is remoting and the basic concepts like Marshalling, Channels, Formatters etc. In the next part, we will go deep into the programming of remoting.