Thursday, June 19, 2008

Remoting in c#

Remoting in c#


What is Remoting?

Remoting is the process through which we can access any remote object from one application
domain to another application domain. We need to build only the following:

  • A remotable object ( Class Library )

  • A host application domain to listen for requests for that object.

  • A client application domain that makes requests for that object.



Remotable Object:
The class must inherit from

MarshalByRefObject
. The following procedure describes how to
create a basic object that can be created and invoked from objects executing in
another application domain.

Create the ClassLibrary through
Visual Studio 2005 IDE . Select File-> New -> Project

Sample Class Code



using System.Collections.Generic;
namespaceRemotingClass
{
public class RemotingClass : System.MarshalByRefObject
{
public int Sum(int a, int b)
{
return a+ b;
}
}
}

Now Compile & Build the assembly Name
as RemotingClass.dll


Listener Form

Build Host Application ( Listener object)


You can build listener applications using any type of application domain — a Windows Forms application, an ASP.NET Web application, a console application, a Windows Service (also known as a Windows NT Service), or any other managed application domain. Because remote configuration is done for each application domain, the application domain must be running to listen for requests.
A sample listener windows form.


Listener Form

Listener Sample Code

First you refer the RemotingClass.dll
Through "Project->AddReference..."

Listener Form




usingSystem.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp
namespace RemotingServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(objectsender, EventArgs e)
{
TcpChannel oTcp = new TcpChannel(8085);

ChannelServices.RegisterChannel(oTcp);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingClass.RemotingClass),"raja", WellKnownObjectMode.Singleton);

lblStatus.Text = "Ready";
}
}
}



Now Compile & Build the assembly Name as RemotingServer.exe


Build Client Application ( Request Application)



The application must register itself as a client for that remote object and then invoke it as though it were within the client's application domain. The .NET remoting system intercepts your client calls, forwards them to the remote object, and returns the results to your client. Desing the Client Application like this.

Listener Form

Then you refer the RemotingClass.dll
For Creating Object Meta data, Through "Project->AddReference..."


Listener Form

Client Application Code ( Request Code )



using System.Windows.Forms;
namespace RemotingClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
RemotingClass.RemotingClass
oRClass = new RemotingClass.RemotingClass();
oRClass = (RemotingClass.RemotingClass)Activator.GetObject(typeof(RemotingClass.RemotingClass), "tcp://localhost:8085/raja");
txtResult.Text = (oRClass.Sum(Int32.Parse(txtA.Text), Int32.Parse(txtB.Text))).ToString();
}
}

}

Now
Compile & Build the assembly Name as
RemotingClient
.exe

How to Invoke the Remoting application.


  • Run the Remoting Server Application ( Listener Application) -> RemotingServer.exe


  • Listener Form

  • After got the ready message invoke the
    RemotingClient
    .exe


  • Listener Form

  • Now Give the 'A' & 'B' Value , Then click Ok.


  • Listener Form


Conclusion

.NET remoting enables you to build
widely distributed applications easily, whether the application components are all
on one computer or spread out across the entire world. You can build client applications
that use objects in other processes on the same computer or on any other computer
that is reachable over its network. You can also use .NET remoting to communicate
with other application domains in the same process.

No comments: