i

ASP.Net A Complete Guide

Remote Method Calls

.NET remoting is a great technology that provides a strong framework for developing distributed applications. The applications that need communications with other .NET components & where performance is the main goal. In such places, Remoting is the best choice. We can use .NET Remoting when sending and receiving data between diferent.NET applications.

Each process or thread of an application will run in AppDomain. If different domain object talks for fulfilling the job, then that process is called remoting. An object is in the same app domain considered as Local Object. If the Object is not in the same app domain, then it is called as remote.

The need for Remoting:

Example:

If we require to display weather forecast information in our application. In such a scenario, we need another AppDomain object of weather class. Remoting will allow us to do this.

Benefits provided by Remoting:

  • It allows communication with the remote object.

  • It reduces the number of lines of code.

  • PerformanIce benefits.

Implementation of remoting

The remote object is implemented by deriving the System.MarshalByRefObject. by using this, we can enable the remote access of objects across application domains. A remote object will have limited access to the application domain where it is created. In the remoting, a client will not be able to call the methods directly; instead, a proxy object will be created and used to invoke methods on the remote object. Each public method that we create in the remote object class is available for the client to call.

 Working of Remote Object:

  • When the client gives calls to the remote method, the proxy receives the call, encodes the message using an appropriate formatter.

  • It then sends the call on the channel to the server process.

  • A listening channel then picks up the request & forwards it to the server remoting system, which determines and invokes the methods on the requested object.

  • Once the execution gets complets, the process is reversed & the results will get returned back to the client.

The remoting framework comes with below formatters:

The binary formats:

  • This formatter is extremely fast and encodes method calls in a proprietary, binary format.

The SOAP formatter:

  • It is a slower formatter.

  • It allows coders to encode the remote messages in a SOAP format.

Types of Remote Objects:

Client-activated objects:

  • This type of object is a server-side, and its creation and destruction are managed by the client application.

  • The client calls the new operator on the server object if an instance of a remote object is created.

  • The instance alive until the client needs it, and lives across one to many method calls.

  • The object will be subject to garbage collection once it's determined that no other clients need it.

Server-activated objects:

This type of object's lifetime is managed by the remote server.

The server-activated objects are not created when a client calls New or Activator.GetObject. They are rather created when the client actually invokes a method on the proxy. There are two types of server-activated objects.

Creating a remote object:

To create a remote object, we need to inherit from MarshalByRefObject class. The following example shows a remotable class.

Example:

using System;

namespace RemoteClassLib

{

   public class MyRemoteObject : System.MarshalByRefObject

   {

      public MyRemoteObject()

      {

         Console.WriteLine("Constructor called");

      }

      public string Welcome(string name)

 

      {

         Console.WriteLine("Welcome Called");

         return "Welcome " + name;

      }

   }

}