|
.Net Remoting Overview
<<Previous
Next >>
By Nishant Kumar
Distributed application is a set of components distributed across network
and work as if all the components are there on the same computer. These
components based technologies can be developed using DCOM, CORBA, RMI etc.
These technologies are good for intranet environment.
Microsoft has provided suitable framework for developing distributed
application through .Net Remoting and Web Services. Remoting.net allows
components to interact across application domains, processes, and machine
boundaries. It enables your applications to take advantage of remote resources
in a networked environment.
Web Services are best suited when clients outside the firewall calling
components on the server over the Internet.
Remoting.Net is the best solution when clients and components are inside the
firewall. It requires the client to be built using .NET which means it can’t
work in heterogeneous environments.
.Net Remoting uses channels (HTTP and TCP channels) to transport messages to
and from remotable objects. The HTTP channel uses the SOAP protocol to
transport messages which means all messages are serialized to XML. The TCP
channel uses a binary stream to transport the messages.
.Net Remoting supports two activation modes: Singleton and SingleCall.
Singleton mode allows only one instance of an object at any time which means
all requests are serviced by the single instance. You can maintain state across
each request using this mode. SingleCall Mode creates a new instance of the
object for each client request and are stateless.
.NET Remoting when hosted with IIS, can use all the security features of
ASP.NET. But you need to implement your own security features for the
application if channel is hosted in the process other than
aspnet_wp.exe.
When to use .Net Remoting over Web services
.Net Remoting provides distributed solution for a corporate use. So, if you need
distributed environment to be implemented for internal use, .Net remoting is
the best choice. If you require a faster distributed solution, .Net Remoting
with TCP protocal using binary format is faster than Web services. .Net
Remoting is better solution when large transfer of data is required.
How does .Net Remoting works?
Remoting.Net allows components to
interact across application domains, processes, and machine boundaries, thus
allows applications to access remote resources in a networked environment. The
interaction of components is made possible through proxy in remoting
architecture. When a client calls the remote method, it’s the proxy that
receives the call. The proxy then encodes the message using formatter. The
messages are then sent over the channel to the server process where listening
channel receives the call and passes it to the remoting system. The requested
method is then invoked and results are returned back to the client.
.Net Remoting provides an
infrastructure where objects of different AppDomains can interact. A client
interacts with server object using .Net Remoting architecture. An object
interacts with other objects outside AppDomains using proxy since the objects
can't access directly anything outside AppDomain.
Point to be noted.
A remote object is implemented by inheriting MarshalByRefObject class.
A client has to obtain proxy activating a remote object by calling
CreateInstance, GetObject, or new.
Local objects can be passed as parameters when making remote calls. Local
objects are passed by value in a remote call.
The object passed as parameter in a remote call must be serialized.
Activation Model
You need to activate remote object before use. There are two activation modes
in .Net Remoting
Server Activation
In this mode, objects are created automatically when a client attempts to
access the object. The object doesn't get created when you use new keyword to
create instance of the server class.
Client Activation
In this mode, objects are created when you use new keyword to create instance
of the server class.
A server object is created and
deployed on the network that serves client requests. The server objects have to
be registered with the CLR before it can be accessed by client. The details
that have to be provided to the CLR are
Name of the assembly that should
be loaded to activate the object
The namespace and type name of the object
The name of the endpoint where the object can be accessed
The channels to be used by client to communicate have to be registered.
The registered channels then start listening for clients to connect.
Once a remote object has been
deployed, clients can connect and invoke methods on the server object.
In order to access remote object,
the client first activates the object by calling new, GetObject, or
CreateInstance. On activation request, a proxy is created to represent the
remote object. The client message in the serialized form is transported to the
server. The type of serialization depends on the channel. For example, when the
HTTP channel is used, all messages are serialized to XML and transported over
SOAP. On the other hand, TCP uses binary serialization.
On the server side, the requested
method is then invoked and results are packaged in a message and returned back
by to the client. If the target object is of type SingleCall, it will
automatically be garbage collected after the call completes.
Approaches to access server objects in Remoting.Net
First one to have copy of server
object on the client machine and accessing local copy of object to call method.
This method is good suited when the object is not very big not having too many
methods. Copying big object is wastage of client resources which includes
network resources and processing time.
The second approach is to create a proxy object that returns reference of all
the methods and properties of server object in the client domain. The proxy
acts as local object, fake server object on the client machine. Any method call
from the client will be served by proxy which in turn access server domain to
get response from server object. This approach is good when the object is big
with many methods.
Remotable and Nonremotable objects
You have two categories of
objects in distributed applications: Remotable and Nonremotable objects.
Nonremotable object can’t be accessed outside its own application domain. This
kind of object doesn't allow its methods or properties to be used by remoting
system. Remotable objects can be accessed outside its own application domain.
The remoting system can use methods and properties of this kind of object.
There are two types of remotable objects
Marshal-by-value-objects -
When client calls a method on marshal-by-value-object, the remoting system
creates a copy of this object and passes the copy to the client application
domain. The copy hence received can handle any method call in client domain.
Using Marshal-by-value-object reduces resource consuming trip across network.
Marshal-by-reference-object -
When client calls a method on Marshal by reference object, the remoting system
create proxy object in the caller application that contains the reference of
all method and properties of the object.
Create a remote object
To create remote class, you need to inherit from MarshalByRefObject class.
Imports System.Runtime.Remoting
Public Class MyRemoteObj Inherits MarshalByRefObject
Public Function Welcome(ByVal Name
as String)
Console.WriteLine("This is my first remoting object")
return
"Welcome " + name
End Function
End Class
In the above code, we have created a class that inherits from
MarshalByRefObject. Now we create a host application that hosts the remote
object. This host application will read detail of remote object from
configuration file and wait for the client to connect to it.
Imports System
Imports System.Runtime.Remoting
Public class Test
Public Shared Sub Main()
RemotingConfiguration.Configure("MyApp.exe.config")
Console.WriteLine("Press
return to Exit")
Console.ReadLine()
End Sub
End Class
Here, the application read the configuration file using
RemotingConfiguration.Configure method.
Here is code for configuration file, MyApp.exe.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown mode="SingleCall"
type="MyRemoteObj,MyRemoteObj"
objectUri="MyRemoteObject">
</wellknown>
</service>
<channels>
<channel ref="tcp server" port="8080"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
In the configuration file, we have exposed remote object using TCP channel.
Once the hosting application is started, then client applications can start
creating instances of the remote object and invoke its methods.
Important points for .Net Remoting
.Net Remoting produce good performance in terms of speed when use with TCP
channel and the binary formatter.
.Net remoting when hosted in IIS, can benefit from all the security features of
IIS. If you use TCP or HTTP channel hosted in processes other than
aspnet_wp.exe, you need to implement security features on your own.
.NET Remoting supports state management with Singleton objects.
This article includes brief about
ASP.NET, advantages of ASP.NET, navigation sequence of ASP.NET web form, web
Form components, .NET framework, event handlers in ASP.NET, web form events,
server control events in ASP.NET, and server controls vs. HTML controls,
validation controls, navigation, and steps to store cookies, ways to
authenticate and authorize users in ASP.NET etc.
This includes introduction of .Net framework, .Net framework architecture, role
of assembly and GAC.
This includes explanation of code security, Principal object, declarative and
imperative security, role-based security, code access security and code group.
This article explains .Net assembly, private and shared assembly, satellite
assemblies, resource-only assembly, ResourceManager class, strong name, global
assembly cache.
Here you can learn about break mode, options to step through code in .Net, Debug
Vs Trace in .Net, trace class, listeners collection of Trace and Debug objects
and Trace Switches.
This article explains about authentication, authorization, authentication mode
and impersonation in ASP.NET.
This includes caching mechanism in ASP.NET, its advantages and types.
Here you have details about exception handling and ways to handle exception in
ASP.NET. It also includes brief detail of Try/catch block, Error Events
and Custom Error Pages.
Here you have description of globalization, localization and their approaches in
ASP.NET. It also describes resource files and satellite assemblies.
This article has content about master page, its advantages and how to create
master page in ASP.NET. It also describes about multiple master page.
Here you have details about session state, its modes and advantages and
disadvantages of using session state management in ASP.NET.
This article describes state management in ASP.NET. It explains client-side
state management and server-side state management.
This includes description about validation control, its types and steps to use
them in ASP.NET.
|