Net Remoting
Built Components
------------------
1. A remotable object. (dll, MarshalByRefObject)
2. A host application to listen for client requests for that object. (any form) (tcpchannel, httpchannel)
3. A client application domain that makes requests for that object.
.NET Remoting
--------------
Register channels(4) in server using RemotingConfiguration class.
1-RegisterActivatedClientType
2-RegisterActivatedServiceType
3-RegisterWellKnownClientType
4-RegisterWellKnownServiceType
Activated =client activation
Wellknown =Server activation
Server activation
-------------------
1. Singleton : one object in server serves all request and all clients.
2. SingleCall: an object will be created for each client request.
what to pass between server & client
------------------------------------
1. Type of class. typeof(RemoteObject.RemoteObject)
2. uri object. Give URL a reference for all request.
3. channel info. WellKnownObjectMode.Singleton
Activation
----------
Before remote object can be accessed, object instance needs to be created/initialized.
eg.
(RemoteObject.RemoteObject)Activator.GetObject(typeof(RemoteObject.RemoteObject), "tcp://localhost:8000/DotNetRemoting");
RemoteObject.dll
namespace RemoteObject
{
public class RemoteObject : MarshalByRefObject
{
public RemoteObject()
{
Console.WriteLine("Remote Object created");
}
public string PrintYourAddress()
{
string strAddress = AppDomain.CurrentDomain.BaseDirectory;
Console.WriteLine("This is myaddress " + strAddress);
return strAddress;
}
public string WriteInServer()
{
return string.Format( "I wrote time in server {0} " , DateTime.Now.ToShortTimeString());
}
}
}
|
Host.exe (RemoteObject.dll)
class HostServer
{
static void Main(string[] args)
{
TcpChannel Channel = new TcpChannel(8000); //create server channel to listen at port 8080
ChannelServices.RegisterChannel(Channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject.RemoteObject), "DotNetRemoting", WellKnownObjectMode.Singleton);
System.Console.WriteLine(" Listening for request from client at 8080. Press the enter key to exit...");
System.Console.ReadLine();
}
} |
Client.exe (RemoteObject.dll) private void Form1_Load(object sender, EventArgs e)
{
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
retobj = (RemoteObject.RemoteObject)Activator.GetObject(typeof(RemoteObject.RemoteObject), "tcp://localhost:8000/DotNetRemoting");
}
private void btn1_Click(object sender, EventArgs e)
{
MessageBox.Show( retobj.WriteInServer());
}
private void btn2_Click(object sender, EventArgs e)
{
string str = retobj.PrintYourAddress();
MessageBox.Show(str);
} |