Overview
The replacement of DCOM is .NET Remoting. .NET Remoting can be described as:
- Web Services Anywhere
- CLR Object Remoting
Using SOAP and HTTP are just one way of calling remote objects out of UDP, IPX and SMTP etc.
CLR object remoting sits on top of Web service anywhere.
All the language constructs can be used with remote objects.
Description
.NET remoting is used to access objects in another application domain. In remote assemblies, the client receives a proxy to talk to. Proxy is a representation of a remote object in the client process, used to call methods. The message is passed to the remote object into the channel.
Architecture of .NET Remoting
- Remote Object: Remote Object is one, running on the server
- Channel: Communication path between client and server
Two types of channels for communication are TCP and HTTP.
Custom channels using different protocols are also possible.
- Message: Messages hold the information about the remote object including name and all of the arguments.
- Formatter: Formatters define how messages are transferred into the channel. Default is SOAP and binary formatters.
- Formatter Provider: Formatter Provider associates a Formatter with a channel to send message onto wire.
- Proxy Object: Proxy Objects are of two types:
- Transparent
- Real
Transparent proxy is like a remote object to the client. Transparent proxy calls invoke ()
method on the Real proxy.
- Message Sink: Message Sink is an interceptor on both the client and server. A message sink is associated with the channel. The
invoke ()
method uses the messages sink to pass the message to the channel. Real proxy (proxy at the server) uses the message sink to pass the message into channel, so the sink can do some interception before the message goes into the channel. - Activator: Activator is used by the client to get proxy of a server- activate (already created) object.
Remoting
- Configuration: Remote Configuration is used to configure servers and clients both dynamically or by using configuration files.
- Channel Services: Channel Services are used to register channels and dispatch messages to them.
Client Side Working
When the client calls on a remote object, transparent proxy methods are called. Transparent proxy then calls the real proxy. The real proxy is responsible for sending the message to the channel. Real proxy in turn locates the message sink and passes the message to the sink. Message sink sends the message into the channel. Formatter formats the messages to be sent on wire. Channel is responsible for connecting to the listening socket on the server and sending the formatted data.
Server Side Working
Server channel receives the formatted messages and formatter un-marshals messages. Channel calls server-context sinks (message sinks associated with server), which in turn call object-context sinks (object sinks associated with an object). Last object-context sink calls the method in the remote object.
What is Context
An application domain can have different contexts. A context is used to group objects with similar execution requirements.
To bind an object to an application domain MarshalByRefObject
is used. While ContextBoundObject
which drives from MarshalByRefObject
binds an object to context. Outside the context, proxy is needed to access the object.
Context attributes is assigned to objects derived from ContextBoundObject
. Custom attribute can be created by implementing IContextAttribute
.
Distributed applications components:
- Remote Objects
- Clients
- Servers
Remote Objects
An object that should be called remotely from a different system must be derived from System.MarshalByRefObject
.
Public class Hello:System.MarchalByRefObject
{
Public Hello()
{
}
~Hello(){
}
Public string Greeting(string name)
{
}
}
The TCPServerChannel
is created with some port to register it with Channel Services. Remote Object is then registered with RegisterWellKnownServiceType
.
Client
In client program, we are creating TCPClientChannel
to get the free port for communication. Activator is used to return a proxy to the remote object.
Public class HelloClient
{
Main()
{
ChannelServices.RegisterChannel ( new TCPClientChannel() );
Hello obj = (Hello) Activator.GetObject(type of(Hello),
"tcp://localhost:8086/Hi");
If ( obj == null)
{}
for( int i=0; i < 5; i++) { obj.Greeting("");}
}
}
History
- 2nd January, 2008: Initial post