Click here to Skip to main content
13,345,254 members (46,257 online)
Click here to Skip to main content
Add your own
alternative version


49 bookmarked
Posted 19 Mar 2002

Remote Objects Part 1

, 19 Mar 2002
Rate this:
Please Sign up or sign in to vote.
This demo shows how to create remote objects with callback functions and use them in the window host.
<!-- Download Links -->



<!-- Add the rest of your HTML here -->


.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.

The server object that has the ability to handle the callback is very important for some type of service. For example, when user logon the Auction server to bid an item, the user need to know the current price of the item. This requires the Auction Server to have the callback function to send the price information to all the users they are bid on the item.

In this article we only discuss how to use event handler to register the client's callback function (My next article “Remote Object Part 2” will discuss the client sink its instance to the server). 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.

 SrvObj.SrvMessaging += new ServerEventHandler(CltObj.ServerMsgHandler);

This code add event handler CltObj.ServerMsgHandler to the event SrvMessaging on SrvObj.

The delegate is derived from System.MupticasetDelegate, which has a linked list that can hold any number of the event handler function. So, one event can have multiple event handlers. This means one event on the server object can handle all the callback function of the clients that is registered on that event.

The disadvantage of using event handler to register callback is not flexible for clients and the Server side can’t control over the clients to register callback function. If server wants to add more event handlers for different purpose, the client program has to change its code to use these new event handlers.

Server Object:

namespace WremoteServer                                                              
      // Define the event                                                            
      public delegate void ServerEventHandler (object sender, RemoteEventArgs e);

      public class RemoteServer : MarshalByRefObject
            public static event ServerEventHandler InvokeHostForm;
            public event ServerEventHandler SrvMessaging;
            public event ServerEventHandler 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)                                  
                  RemoteEventArgs e = new RemoteEventArgs(str);                      
                  // Fire Client Event                                               
                  if (SrvMessaging != null)                                          
                        SrvMessaging(this, e);                                       
            public void SendMsgToClient2(string str)                                 
                  RemoteEventArgs e = new RemoteEventArgs(str);                      
                  // Fire Client Event                                               
                  if (SrvMessaging2 != null)                                         
                        SrvMessaging2(this, e);                                      

Client Object:

namespace WremoteClient                                                              
      public delegate void CFormEventHandler(object sender, RemoteEventArgs e);      
      public class RemotingClient : MarshalByRefObject                               
            public event CFormEventHandler InvokeFormMethod;                         
            public void ServerMsgHandler(object sender, RemoteEventArgs e)           
                  if(InvokeFormMethod != null)                                       
                        InvokeFormMethod(this, e);                                   

Client host:

// connect to server object without register callback                                
private void ConnSrv_Click(object sender, System.EventArgs e)                        
            if (registedChan != null)                                                
                  registedChan = null;                                               
            HttpChannel chan = new HttpChannel(Int32.Parse(port.Text));              
            registedChan = chan;                                                     
            SrvObj = (RemoteServer)Activator.GetObject(                              
            if (SrvObj == null)                                                      
                  lb.Items.Add("Can't connect to server");                           
                  string strServer;                                                  
                  SrvObj.RemoteServerMethod(tb.Text + " from the client",            
                                            out strServer);    
      catch(Exception e2)                                                            

// register callback on the msg1                                                     
private void ForMsg1_Click(object sender, System.EventArgs e)                        
      if(callbackFlg == false && registedChan != null)                               
                  Type.GetType("WRemoteClient.RemotingClient, RemotingClient"),      
            string url=String.Format("http://localhost:{0}/ClientObj", port.Text);   
            CltObj = (RemotingClient)Activator.GetObject(                            
            SrvObj.SrvMessaging += new ServerEventHandler(CltObj.ServerMsgHandler);  
            CltObj.InvokeFormMethod += new CFormEventHandler(OnMsgHandler);          
            string strServer;                                                        
            SrvObj.RemoteServerMethod("Callback is registed!", out strServer);       
            callbackFlg = true;                                                      

Steps to run the sample:

  1. Compile the Remote object by running makefile.bat at RemoteObj directory.
  2. Compile the WremoteClient and the WremoteServer. You may need to change the references to these DLL.
  3. Copy all the DLL files in RemoteObj directory to the WremoteClient\bin\Debug and WRemoteServer\bin\Debug directory.
  4. Run WRemoteServer.exe
  5. Run WRemoteClient.exe, then Click Connect Server, after this, you can send message to server and get the return message.
  6. Click register callback to select which message do you want to registered, after this, the client is waiting for the server message on the open channel.


When you want to start more than one client on the same machine, You should select different port number for the channel that is reserved for the callback.

This demo is updated with .Net framework 1.0 Version 1.0.3705. The Beta2 user may need to replace the control with Beta2 to make it work.

If you have any comments, I would love to hear about it. You can reach me at Jibin Pan.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jibin Pan
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Questiontrouble cross threading in .NET vs 2005 Pin
bibiboule2-Jan-07 23:03
memberbibiboule2-Jan-07 23:03 
GeneralClosing client causes error Pin
ty197510-Jan-05 17:55
memberty197510-Jan-05 17:55 
GeneralError- 'Specified Cast Not Valid' while accessing remote COM objects Pin
Anonymous10-Oct-04 23:58
sussAnonymous10-Oct-04 23:58 2003 Pin
dariush_hk18-Oct-03 5:11
memberdariush_hk18-Oct-03 5:11 
GeneralSecurity Problem in 1.1 Pin
pastoda19-Aug-03 6:57
memberpastoda19-Aug-03 6:57 
GeneralRe: Security Problem in 1.1 Pin
sympthom 928-Dec-03 4:43
membersympthom 928-Dec-03 4:43 
I have .NET 1.1 and it doesn´t work on my machine. How do I do to make it work?Confused | :confused:
GeneralRe: Security Problem in 1.1 Pin
Anonymous19-Dec-04 8:41
sussAnonymous19-Dec-04 8:41 
Generalproblem Pin
Rumen Yankov16-Nov-02 0:08
memberRumen Yankov16-Nov-02 0:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 20 Mar 2002
Article Copyright 2002 by Jibin Pan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid