SIGN UP MEMBER LOGIN:    
ARTICLE

Remote Objects:Part I

Posted by Jibin Pan Articles | .NET Remoting in C# March 12, 2003
This demo shows how to create the remote objects with callback functions and use them in the window host.
Reader Level:
Download Files:
 

This demo shows how to create the remote objects with callback functions and use them in the window host. 

.NET Remote objects can be hosted in the .NET Windows EXE server, the Internet Information Server (IIS) and the .NET component services. Using object references to communicate between server objects and clients is the heart of remote. The remote objects are reusable and lets you design a business logic object model according to the needs of the application. 

The real strength of the remote system is its ability to transport messages between remote objects in different application domains or processes using different transportation protocols, serialization formats, object lifetime schemes.

In this article we only discuss how to use event handler to register the client's callback function. The Server Object declare three events that can be used by the client to register the event handler in their host or register their callback function with which client can be called when the event raise. The clients use server's event handler to register their callback function is very easy.

Server Object:

namespace WremoteServer
{
// Define the event
public delegate void RegisterEventHandler(object sender,RemoteEventArgs e);
public
class RemoteServer : MarshalByRefObject
{
public static event ServerEventHandler InvokeHostForm;
public event RegisterEventHandler SrvMessaging;
public event RegisterEventHandler SrvMessaging2;
public void RemoteServerMethod(string str, out string strOut)
{
string tmp=string.Format("RemoteServerMethod is called: {0}",str);
RemoteEventArgs e = new RemoteEventArgs(tmp);
// Fire server form event
if(InvokeHostForm != null)
InvokeHostForm(this, e);
strOut = str + " Returned from Server!";
}
public void SendMsgToClient(string str, int msg)
{
RemoteEventArgs e = new RemoteEventArgs(str);
// Fire client form vent
if (InvokeCallbackMethod != null && (msg == 1 || msg==0))
InvokeCallbackMethod(this, e);
else if (InvokeCallbackMethod2 != null && (msg == 2 || msg==0))
InvokeCallbackMethod2(this, e);
}
}
}

Server host: 

private void startServer()
{
ListDictionary channelProperties = new ListDictionary();
channelProperties.Add("port", 8085);
HttpChannel chan = new HttpChannel(channelProperties, new BinaryClientFormatterSinkProvider(), new BinaryServerFormatterSinkProvider());
if(ChannelServices.GetChannel(chan.ToString()) == null )
ChannelServices.RegisterChannel(chan);
try
{
RemotingConfiguration.ApplicationName = "RemotingServer";
RemotingConfiguration.RegisterWellKnownServiceType( Type.GetType("WRemoteServer.RemoteServer, RemoteProj"), "RemotingServer.soap", WellKnownObjectMode.Singleton);
sBar.Text = this .GetType().Name + " is ready";
SrvActObj = (RemoteServer)Activator.GetObject( typeof(WRemoteServer.RemoteServer), Http://localhost:8085/RemotingServer/RemotingServer.soap);
}
catch (Exception e2)
{
MessageBox.Show("Register remote fail" + e2.ToString());
}
}

Client host:

// connect to server object without register callback
private void ConnSrv_Click(object sender, System.EventArgs e)
{
try
{
if (registedChan != null)
{
ChannelServices.UnregisterChannel(registedChan);
registedChan = null;
unRegistAll();
}
HttpChannel chan = new HttpChannel(Int32.Parse(port.Text));
ChannelServices.RegisterChannel(chan);
registedChan = chan;
SrvObj = (RemoteServer)Activator.GetObject( typeof(WRemoteServer.RemoteServer), http://localhost:8085/RemoteObj);
if (SrvObj == null)
lb.Items.Add("Can't connect to server");
else
{
string strServer;
SrvObj.RemoteServerMethod(tb.Text + " from the client", out strServer); lb.Items.Add(strServer);
}
}
catch(Exception e2)
{
lb.Items.Add(e2.Message);
}

// register callback on the msg1
private void ForMsg1_Click(object sender, System.EventArgs e)
{
if(callbackFlg == false && registedChan != null)
{
Callback = new MyCallbackClass(this) ;
SrvObj.InvokeCallbackMethod += new RegisterEventHandler (Callback.SubmissionCallback);
string strServer;
lb.Items.Add("Msg1 is registered");
SrvObj.RemoteServerMethod("Callback is registered!", out strServer);
callbackFlg = true;
}
}

Login to add your contents and source code to this article
share this article :
post comment
 

thanks For Your Article

Posted by shahrokh fallah Apr 06, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor