Remoting in .NET
October 3, 2008
Remoting:
Establishing communication between objects that run in different processes, whether on the same computer or on different computers.
.NET Remoting enables client applications to use objects in other processes on the same computer or on any other computer available on its network.
Communicating across processes is still a complex task, but much of it is now handled by the .NET Framework.
To use .NET remoting to build an application in which two components communicate directly across an application domain boundary, you need to build only the following:
- A remotable object.
- A host application domain to listen for requests for that object.
- A client application domain that makes requests for that object.
Remote Objects:
There are three types of objects that can be configured to serve as .NET remote objects.
They are: 1) Single Call Objects
2) Singleton Objects
3) Client Activated Objects (CAO)
Code:
RemoteComponet Library:
//remote component library
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
namespace RemoteComponentLib
{
public class RemoteComponent:MarshalByRefObject //MarshalByRefObject ensures that the only reference of the object sent not the cpoy of the object
{
public string Greet(string name)
{
Debug(name);
return “Hello” + name + “from satyam”;
//Expected output is
//#3448748-user1 -at server side
//hello user from satyam -sent to client
}
//for tracing
private void Debug(string name)
{
Console.WriteLine(“#{0}-{1}”, this.GetHashCode(),name);
}
}
}
Remote Client Console type:
//remote client program console type
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting; //provides classes and interfaces that allow developers to create and configure distributed applications.
using RemoteComponentLib; //connects to library
namespace RemoteClient
{
class Program
{
static void Main(string[] args)
{
RemotingConfiguration.RegisterWellKnownClientType(typeof(RemoteComponent),“tcp://localhost:9999/RemoteUri”);
RemoteComponent obj = new RemoteComponent();//the created obj refers as proxy
Console.WriteLine(“Enter a string”);
string str = Console.ReadLine();
Console.WriteLine(obj.Greet(str));
Console.ReadLine();
}
}
}
Remote Host:
//program for the RemoteHost
//remote client program console type
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting; //provides classes and interfaces that allow developers to create and configure distributed applications.
using RemoteComponentLib; //connects to library
namespace RemoteClient
{
class Program
{
static void Main(string[] args)
{
RemotingConfiguration.RegisterWellKnownClientType(typeof(RemoteComponent),“tcp://localhost:9999/RemoteUri”);
RemoteComponent obj = new RemoteComponent();//the created obj refers as proxy
Console.WriteLine(“Enter a string”);
string str = Console.ReadLine();
Console.WriteLine(obj.Greet(str));
Console.ReadLine();
}
}
}
Remote Client windwos type:
//for Remote client in windows form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting; //provides classes and interfaces that allow developers to create and configure distributed applications.
using RemoteComponentLib;//To create objets of type RemoteComponent that acts as proxy
namespace RemoteClib
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//RemoteComponent obj = null;
private void Form1_Load(object sender, EventArgs e)
{
//RemotingConfiguration.RegisterWellKnownClientType(typeof(RemoteComponent), “tcp://localhost:9999/ RemoteUri”);
//obj = new RemoteComponent();//proxy
}
private void button1_Click(object sender, EventArgs e)
{
//RemoteComponent:Provides various static methods for configuring the remoting infrastructure.
//RemotingConfiguration:Overloaded.Registers an object Type on the client end as a well-known type (single call or singleton).
//Registers an object Type on the client end as a well-known type that can be activated on the server,using the given parameters to initialize a new instance of the WellKnownClientTypeEntry class.
RemoteComponent obj = null;
RemotingConfiguration.RegisterWellKnownClientType(typeof(RemoteComponent), “tcp://localhost:9999/RemoteUri”);
obj = new RemoteComponent();//proxy
MessageBox.Show(obj.Greet(textBox1.Text));
Console.ReadLine();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
OutPut:
Different Windows for the following
1. Singleton –console Application
2. Singlecall –console Application
3. SingleCall –Windows Application
4. SingleTon –Windows Application
Aslo:the code can be modified to use over different machines using IP address
Entry Filed under: Uncategorized. .
Trackback this post | Subscribe to the comments via RSS Feed