Simple Remoting Example
In traditional development targeting Microsoft platforms DCOM was the obvious choice for developing distributed applications. However, it had problems of its own such as proprietary standards and security over internet. Under .NET framework Remoting provides an infrastructure for developing and using distributed components. Using remoting multiple AppDomains can talk with each other.
While developing a distributed application the underlying framework should provide transparent programming model to the application developer. Proxies play major role in this area. The proxy object acts as a 'dummy' of the remote object and forward all the method calls to the remote object instance. These proxy objects are created at client end (here client simply means a component which is calling the remote object). The client is unaware of the actual location of the remote object and it simply calls the methods on the proxy object which are then forwarded to the actual object.
Activation of remote objects
The remote objects can be activated in two ways
- Server Activation
- Client Activation
In the server activation managing the life time of the remote object is not in the hands of client where as client activation involves life time management of the remote object.
The server activated objects are of two types:
- Single call objects : These objects are state less objects i.e. they do not maintain state across method calls
- Singleton objects: These objects share the same instance of object among various clients and also maintain state across method calls. Here various clients share the same state information.
Client Activated Objects are created for individual clients and can store state information between method calls for that specific client.
Leased based life time
We just mentioned that the client activated objects can control the life time of the remote objects. The mechanism by which it happens is called as Leasing. The remote objects have a default lease period. When the lease expires the object is garbage collected. The client can control the lease period by following ways:
- The server object can set its lease time to infinity
- The client can renew the lease of the object
- The client can make use of a sponsor which in turn requests a lease renewal
The communication between the client and remote object involves method parameters and return values. This data must be serialized before it is sent across the network. The serialization process creates a persistent copy of the data as a sequence of bytes. The process of converting these bytes back into the original data structure is called de-serialization. .NET remoting framework provide special objects for performing this serialization and de-serialization. They are called as Formatters. Formatters are available in following namespaces:
As the names suggest Binary formatter deals with data in binary format while SOAP formatter deals with data in XML format.
To communicate with remote object we need a path through which the data transfer can take place. This path is called as Channel. There are two types of channels:
- TCP channels : As the name suggests these channels us TCP for communication. TCP channels typically carry data in binary form i.e. they use Binary formatters
- HTTP Channels : These channels use HTTP for communication. They typically carry SOAP messages i.e. they use SOAP formatters.
TCP channels with Binary formatters are suitable where speed and performance is important where as HTTP Channels with SOAP formatters are suitable for platform independent or over the web communication.
The channels are available in following namespaces:
You can create your own channels for gaining more control over the transmission process.
How it works?
When the client application communicates with the remote object following steps are involved:
At Server Side
- A Channel is established at a specific port which will listen all the client requests
- Remote object registers itself with remoting framework and declares its presence over the network
At Client Site
- Client establishes a channel at a specific port to talk with remote object
- Client creates an instance of remote object by calling Getobject or CreateInstance or new method.
- Client gets the proxy for the remote object
- Client calls remote object methods on the proxy
The download provided with this article contains three sample applications:
- Remotable Component (Server)
- Remote Component Publisher
In the sample the remote component provides Credit Card Validation service using publicly available Luhn logic. Note that this logic in no way guarantees that the number you provided a real credit card number. For example, the number 000000000000000 (Sixteen 0s) is valid credit card number as per Luhn logic! The publisher application publishes this component over network with a unique URI and port. The client then gets service from this object by using the URI.