Thursday, February 4, 2010

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

0 Comments: