Click here to Skip to main content
12,360,037 members (61,872 online)
Click here to Skip to main content
Add your own
alternative version


63 bookmarked

A simple Remoting HelloWorld

, 1 May 2005
Rate this:
Please Sign up or sign in to vote.
In this article, I will explain how to do remoting in VB.NET using Framework 1.1.

Sample Image - Remoting_HelloWorld.jpg


Searching a lot, I didn't found a really easy remoting sample to show in the courses that I do, so I decide to publish my own article on how to do remoting in an easy way.

First of all, this article is based in VB.NET using .NET Framework 1.1 (Visual Studio 2003).

I will explain how can you do communication with components using Remoting, I will do a brief introduction to remoting now:

At old times, when application development started, we worked with COM, which allowed us to separate UI applications from functional libraries (DLLs). Later we received DCOM that allowed us to communicate between two different components in different computers. DCOM is a very proprietary RPC-based communication protocol for COM-based distributed component architectures. Even though DCOM allows us to create scalable and reliable architectures in the Intranet environment, there are a lot of problems with DCOM when you try to integrate it with different platforms and technologies in an Internet environment.

Now with the new .NET technologies, we receive WebServices and .NET Remoting. The web services allow us to communicate between different platforms and technologies, using standards HTTP and SOAP, that are very good, and very easy to implement. But web services lack some points when you want to work with State Management with .NET. Remoting provides support for both stateful and stateless environments through Singleton and SingleCall objects.

Talking about Type Systems Web services support only the data types defined in the XSD type system, limiting the number of objects that can be serialized, while .NET Remoting uses binary communication, providing support for rich type system. Of course that Web Services has some better points than .NET Remoting, as for example the Interoperability, providing support to interoperability across platforms (ideal for heterogeneous environments), while .NET Remoting requires the client be built using .NET.

Though both the .NET Remoting infrastructure and ASP.NET Web services can enable cross-process communication, each is designed to benefit a different target audience.

For applications that require interoperability and must function over public networks, Web services are probably the best bet. For those that require communications with other .NET components and where performance is a key priority, .NET Remoting is the best choice.

Now let's talk about our solution!! Because Remoting object will not be active by default, we need to create an Application Host Domain (this is how MS calls them), in order to load our exposed library in memory, and expose it as a Remote object. Our solution will consist of four projects, as follows:

  1. IRemoteLib: This will be a sample interface, that will be used by the client and the server part.
  2. LibraryExposed: is the IRemoteLib implementation and it will inherit from MarshalByRefObject, here are all the methods used by the client.
  3. EasyInvoke: This is a WinForm application (it's our Application Host Domain implementation) that will load LibraryExposed and will expose it to our client. We will instance our library as Server-activated object as Singleton activation (the difference in a singleton and single call lies in lifetime management. While single-call objects are stateless in nature, singletons are stateful objects, meaning that they can be used to retain state across multiple method calls. Singleton types never have more than one instance at any one time, sharing the same instance between all clients, while SingleCall types always have one instance per client request).
  4. ClientApplication: This is a WinForm application that will call the remote object.

Using the code

Now let's start with source code. First of all, we need to create our Interface (IRemoteLib).

Public Interface IRemoteLib

  ' This event will be used at server to inform every connection
  Event LibCalled(ByVal message As String) 

  ' Call to Hello World Method
  Sub HelloWorldMethod()

  ' Call to Hello World Function
  Function HelloWorld() As String

End Interface

Now let's continue with the implementation (LibraryExposed).

Public Class RemoteLib
  Inherits MarshalByRefObject
  Implements IRemoteLib
  Public Event LibCalled(ByVal Message As String) Implements IRemoteLib.LibCalled
  Public Function HelloWorld() As String Implements IRemoteLib.HelloWorld 
    RaiseEvent LibCalled("Call HelloWorld ")
    Return "Hello World"
  End Function
  Public Sub HelloWorldMethod() Implements IRemoteLib.HelloWorldMethod
    RaiseEvent LibCalled("Call HelloWorldMethod ") 
  End Sub
End Class

As you see everything is very easy, just pay attention at Inherits MarshalByRefObject. This is where we specified that this will be a remoting object. We do it this way, because we want to specify a singleton, and we want that the client access the remote object using a proxy class.

Now let's implement the EasyInvoke (because it's a WinForm, you need first to create a WinForm, just with a ListBox docked at it). This is the LibraryExposed remote object activation:

Dim objServer As IRemoteLib
Private Sub Form1_Load(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles MyBase.Load
    Dim ServiceChannel As IChannel
    Dim serverProv As BinaryServerFormatterSinkProvider = _
             New BinaryServerFormatterSinkProvider 
    Dim clientProv As BinaryClientFormatterSinkProvider = _
             New BinaryClientFormatterSinkProvider
    Dim Props As IDictionary = New Hashtable
    Dim IpInjProvider As New IpInjectorSinkProvider
    serverProv.TypeFilterLevel = _
    Props("port") = "8000"
    Props("name") = "EasyInvoke"
    ServiceChannel = New TcpChannel(Props, clientProv, serverProv)
    RemotingConfiguration.RegisterWellKnownServiceType( _
                  GetType(RemoteLib), _
                  "EasyInvoke", WellKnownObjectMode.Singleton)
    lstConsole.Items.Add("RemoteLib Started as remoting") ' This is the listbox
    Init() '--------->> this will be used to catch the events from remote object
  Catch ex As Exception
    lstConsole.Items.Add("Error:" + ex.Message)
  End Try
End Sub

This was how to register our LibraryExposed as a remote object. Now let's finish our server as an enhancement that will allow us to retrieve which client is connecting and which method is using our remote object.

Private Sub Init()
  Dim server As IRemoteLib
  Dim Channel As System.Runtime.Remoting.Channels.tcp.TcpChannel 
  Dim serverProv As BinaryServerFormatterSinkProvider = _
         New BinaryServerFormatterSinkProvider
  Dim clientProv As BinaryClientFormatterSinkProvider = _
         New BinaryClientFormatterSinkProvider
  Dim props As IDictionary = New Hashtable
  serverProv.TypeFilterLevel = _
  props("port") = 0
  Channel = New System.Runtime.Remoting.Channels.tcp.TcpChannel(props, _
              clientProv, serverProv)
    server = CType(Activator.GetObject(GetType(IRemoteLib), _
             "tcp://localhost:8000/EasyInvoke"), IRemoteLib)
    If server Is Nothing Then
      MsgBox("server not found or bad initialized!")
    End If
  Catch ex As Exception
  End Try
  'This is where we say how to handle events from the remote object
  AddHandler server.LibCalled, AddressOf ReceiveLibCall 

End Sub
<System.Runtime.Remoting.Messaging.OneWay()> _
Public Sub ReceiveLibCall(ByVal message As String)
End Sub

Here we add an enhancement to our server side, just to listen the events from the remote object to display which method is in use by the client at our ListBox (lstConsole).

And now the ClientApplication implementation:

You should create a WinForm with three buttons, one connect button (butConnect), one HelloWorldSub button (butHelloWorldSub), and one HelloworldFunc button (butHelloWorldFunc) and add this code:

Private server As IRemoteLib
Private Channel As System.Runtime.Remoting.Channels.tcp.TcpChannel 
Private serverProv As BinaryServerFormatterSinkProvider
Private clientProv As BinaryClientFormatterSinkProvider
Private props As IDictionary = New Hashtable

Private Sub SetChannel()
  serverProv = New BinaryServerFormatterSinkProvider
  clientProv = New BinaryClientFormatterSinkProvider
  serverProv.TypeFilterLevel = _
  props("port") = 0
  Channel = New TcpChannel(props, clientProv, serverProv)
End Sub
Private Sub Init()
    server = CType(Activator.GetObject(GetType(IRemoteLib), _
           "tcp://localhost:8000/EasyInvoke"), IRemoteLib)
    If server Is Nothing Then
      MsgBox("server not found!")
    End If
  Catch ex As Exception
  End Try
End Sub

And now implement the buttons click:

Private Sub butHelloWorldSub_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles butHelloWorldSub.Click
  Catch ex As Exception
  End Try
End Sub
Private Sub butHelloWorldFunc_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles butHelloWorldFunc.Click
  Catch ex As Exception
  End Try
End Sub
Private Sub butConnect_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles butConnect.Click
  If butConnect.Text = "Connect" Then
    butConnect.Text = "Disconnect"
    butHelloWorldFunc.Enabled = True
    butHelloWorldSub.Enabled = True
    butHelloWorldFunc.Enabled = False
    butHelloWorldSub.Enabled = False
    server = Nothing
    Channel = Nothing
    serverProv = Nothing
    clientProv = Nothing
    butConnect.Text = "Connect"
  End If
End Sub

That's all folks! I hope you enjoy it!


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

Ricardo Fig
Web Developer
Spain Spain
Hi, I develop since I was 10 (ten) years old, I worked on Basic, QBasic, Pascal, Cobol, C, C++ until 17 years old, then I started my carear as Systems Engineer, and I started to do some MS exams, until I got the degree of Microsoft Certified Professional, in that year I got, MCP+Internet, MCSE, and MCSE+Internet and MCT, then I started to work with Java and I got SUN Java certifications also.

Working as developer I did a lot of differents types of applications (integration app, invoice systems, games, etc).

Also I have some knowlegde on SAP Systems, because I installed it and played it a little bit (SAP WAS 6.10, 6.20 and 6.30), in conjunction with SAP XI

Now I am a consultant and traine on Microsoft Dot Net (Also I got the MCSD.NET and MCAD.NET).

You may also be interested in...

Comments and Discussions

Questionpermission to use derivative works Pin
Member 1100140018-Aug-14 4:50
memberMember 1100140018-Aug-14 4:50 
GeneralWould like to experiment your demo on Vista. Pin
SuperMiQi21-Sep-10 6:47
memberSuperMiQi21-Sep-10 6:47 
GeneralCOM based application Pin
kalidoss_mca26-May-10 20:48
memberkalidoss_mca26-May-10 20:48 
GeneralI want to send message to client Pin
kennyuqz29-Dec-09 6:11
memberkennyuqz29-Dec-09 6:11 
GeneralRe: I want to send message to client Pin
esefer16-Jun-10 21:48
memberesefer16-Jun-10 21:48 
GeneralQuery in remoting concept Pin
Jes_Latha13-Oct-08 2:33
memberJes_Latha13-Oct-08 2:33 
GeneralWell Done - Great example Pin
Member 29761938-Oct-08 21:46
memberMember 29761938-Oct-08 21:46 
GeneralSend event from server (easyInvoke) to client Pin
leasy20-May-08 20:32
memberleasy20-May-08 20:32 
Generalabout Singlton Type usage Pin
Member 432084418-May-08 11:43
memberMember 432084418-May-08 11:43 
GeneralError running example Pin
olly.cox23-Aug-07 23:14
memberolly.cox23-Aug-07 23:14 
GeneralRe: Error running example Pin
Member 53486729-Feb-08 11:11
memberMember 53486729-Feb-08 11:11 
QuestionDCOM Program Written C++ Pin
Khosinski27-Jun-07 4:31
memberKhosinski27-Jun-07 4:31 
AnswerRe: DCOM Program Written C++ Pin
Ricardo Fig27-Jun-07 6:33
memberRicardo Fig27-Jun-07 6:33 
Questionis it possible to use remoting in PPC WM5? Pin
chushumor11-Jun-07 1:58
memberchushumor11-Jun-07 1:58 
QuestionPurpose of Remoting? Pin
Radha Krishnan M22-May-07 3:29
memberRadha Krishnan M22-May-07 3:29 
Questionwhat channel is this using? Pin
neodeaths2-May-07 16:45
memberneodeaths2-May-07 16:45 
QuestionProblems with the event's handlers Pin
lennonblanco10-Apr-07 12:44
memberlennonblanco10-Apr-07 12:44 
AnswerRe: Problems with the event's handlers Pin
lennonblanco12-Apr-07 11:24
memberlennonblanco12-Apr-07 11:24 
QuestionServer Reply Pin
Member #384992021-Feb-07 23:24
memberMember #384992021-Feb-07 23:24 
AnswerRe: Server Reply Pin
vanvaerenbergh16-Mar-07 10:50
membervanvaerenbergh16-Mar-07 10:50 
GeneralChange to use http and web page listener Pin
nesr20-Sep-06 5:32
membernesr20-Sep-06 5:32 
GeneralProblem running sample Pin
IkeRoss17-Aug-06 5:01
memberIkeRoss17-Aug-06 5:01 
GeneralRe: Problem running sample Pin
Ricardo Fig17-Aug-06 5:30
memberRicardo Fig17-Aug-06 5:30 
GeneralRe: Problem running sample Pin
IkeRoss17-Aug-06 5:49
memberIkeRoss17-Aug-06 5:49 
GeneralRe: Problem running sample Pin
Ricardo Fig17-Aug-06 5:51
memberRicardo Fig17-Aug-06 5:51 
GeneralRe: Problem running sample Pin
MVale9-Jan-08 1:31
memberMVale9-Jan-08 1:31 
QuestionWhere to place my real code? Pin
bysh7-Jul-06 6:14
memberbysh7-Jul-06 6:14 
AnswerRe: Where to place my real code? Pin
zakirul23-Jan-07 21:15
memberzakirul23-Jan-07 21:15 
GeneralNice one man! Pin
HoraShadow6-Mar-06 12:45
memberHoraShadow6-Mar-06 12:45 
GeneralNeed info on communication Pin
Ex M.A.Tech5-Feb-06 21:27
memberEx M.A.Tech5-Feb-06 21:27 
GeneralWell Done Pin
jcdl1-Feb-06 6:49
memberjcdl1-Feb-06 6:49 
GeneralPlease help me Pin
AmanUllah Khan12-Dec-05 20:23
memberAmanUllah Khan12-Dec-05 20:23 
GeneralPass var. to running applications Pin
rderkis10-Dec-05 8:20
memberrderkis10-Dec-05 8:20 
Question2 - Way ? Pin
Bob-ish27-Jun-05 16:03
memberBob-ish27-Jun-05 16:03 
AnswerRe: 2 - Way ? Pin
tickko10-Oct-05 6:56
membertickko10-Oct-05 6:56 
GeneralNeeds a little Editing. Pin
gregory_may@hotmail.com31-May-05 10:56
membergregory_may@hotmail.com31-May-05 10:56 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 1 May 2005
Article Copyright 2005 by Ricardo Fig
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid