< C# .NET Remoting Program Example | Main | The C++/CLI .NET Remoting Program Example >


 

 

Chapter 12 Part 3:

.NET Remoting

 

 

What do we have in this chapter 12 Part 3?

  1. The VB .NET Remoting program example

  2. Creating the VB .NET Remoting Demo Class Library (DLL)

  3. Creating the Server VB .NET Remoting Console Application

  4. Creating the VB .NET Remoting Client Program

  5. Testing the Whole .NET Remoting VB .NET Program Sample

 

The VB .NET Remoting program example

 

Creating the VB .NET Remoting Demo Class Library (DLL)

 

Create new VB .NET class library and you can use the project and solution names as shown in the following screenshot.

 

Creating the VB .NET Demo Class Library (DLL): create a new project

 

Rename the source file to Demo to reflect the application that will be developed.

 

Creating the VB .NET Demo Class Library (DLL): renaming the source file

 

Add/edit the code as given below. The DemoClass tries to demonstrate the Marshal by reference while the use of Serialize attribute for the InformationBucket class demonstrates the Marshal by value in .NET remoting programming.

 

Imports System

 

' DemoClass is a very simple class that demonstrates Marshal by Value and

' Marshal by Reference in .Net remoting.

 

' Marshal by Value

<Serializable()> _

Public Class InformationBucket

    Public Value As Integer

    Public Message As String

End Class

 

' Marshal by Reference

Public Class DemoClass : Inherits MarshalByRefObject

    Private Value As Integer = 0

    Private Message As String = ""

 

    Public Sub SetValue(ByVal InputValue As Integer)

        Value = InputValue

        Console.WriteLine("In DemoClass: Setting Value to {0}", InputValue.ToString())

    End Sub

 

    Public Sub SetMessage(ByVal InputMessage As String)

        Message = InputMessage

        Console.WriteLine("In DemoClass: Setting Message to {0}", InputMessage)

    End Sub

 

    Public Function GetCurrentInformation() As InformationBucket

        Dim Bucket As InformationBucket = New InformationBucket

 

        Bucket.Message = Message

        Bucket.Value = Value

 

        GetCurrentInformation = Bucket

    End Function

End Class

 

Build the project and make sure there is no error else you need to resolve the error first.

 

Creating the VB .NET Demo Class Library (DLL): building the project

 

The DLL file should be generated and ready for use.

 

Creating the VB .NET Demo Class Library (DLL): the generated DLL seen from the project physical path

 

Creating the Server VB .NET Remoting Console Application

 

Create new VB .NET console application. You can use the project and solution names as shown in the following screenshot. This server program should be deployed on the server side in the real deployment.

 

Creating the Server VB .NET Console Application: creating new console project

 

Rename the source file to Servervb to reflect the application to be developed.

 

Creating the Server VB .NET Console Application: renaming the source file

 

Add/edit the code as given below. The communication detail such as protocol, port number and the DLL to be searched and loaded is stored in the servervb.config file which will be created later.

 

Imports System

Imports System.Runtime.Remoting

 

' ConfigFileRemotingServer

Class ConfigFileRemotingServer

 

    ' Demonstrates how to develop a server to sponsor a remote object

    <STAThread()> _

    Shared Sub Main()

        Console.WriteLine("Server: Reading the config file...")

        RemotingConfiguration.Configure("..\..\servervb.config", False)

 

        Console.WriteLine("Server: I'm ready! Press return to exit the server")

        Console.ReadLine()

    End Sub

End Class

 

Let build the project to see is there any 'bastard' error. Build the project.

------ Build started: Project: ServerVB, Configuration: Debug Any CPU ------

c:\WINDOWS\Microsoft.NET\Framework\v3.5\Vbc.exe /noconfig /imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Generic,

System.Data,System.Diagnostics,System.Linq,System.Xml.Linq /optioncompare:Binary /optionexplicit+ /optionstrict:custom /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /optioninfer+ /rootnamespace:ServerVB /doc:obj\Debug\ServerVB.xml /define:"CONFIG=\"Debug\",DEBUG=-1,TRACE=-1,_MyType=\"Console\",PLATFORM=\"AnyCPU\"" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll","C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll",c:\WINDOWS\Microsoft.NET\Framework\

v2.0.50727\System.Data.dll,c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll,

c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll,

c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll,"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll" /main:ServerVB.Servervb /debug+ /debug:full /filealign:512 /out:obj\Debug\ServerVB.exe /resource:obj\Debug\ServerVB.Resources.resources /target:exe Servervb.vb "My Project\AssemblyInfo.vb" "My Project\Application.Designer.vb" "My Project\Resources.Designer.vb" "My Project\Settings.Designer.vb"

vbc : error BC30420: 'Sub Main' was not found in 'ServerVB.Servervb'.

========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

Well, as happened in Visual Studio/Express Edition 2005, this thing still happens. Invoke the project property page.

 

Creating the Server VB .NET Console Application: invoking the project property page

 

Change the Startup object: to Sub Main. The previous error should be resolved.

 

Creating the Server VB .NET Console Application: changing the Startup object to Sub Main

 

 

Creating the Server VB .NET Console Application: the standard startup object is Sub Main

 

Invoke the Add New Item page.

 

Creating the Server VB .NET Console Application: invoking the Add New Item page

 

Select the Web Configuration File template and you can use servervb.config as the file name if you want.

 

Creating the Server VB .NET Console Application: adding the Web Configuration File to the existing project

 

Add the following remoting schemas at the end of the file just before the </configuration> tag. These schemas represent the DLL to be searched and loaded, protocol and port number to be used and listened. The remoting schema details can be found in Remoting Settings Schema.

 

  <system.runtime.remoting>

    <application>

      <service>

        <activated type="DemoVB.DemoClass, DemoVB" />

      </service>

      <channels>

        <channel ref="tcp" port="4444" />

      </channels>

    </application>

  </system.runtime.remoting>

 

 

 

 

Creating the Server VB .NET Console Application: the configuration file content

 

Re-build the project and make sure there is no error else please correct it.

 

Creating the Server VB .NET Console Application: rebuilding the project

 

Next, as set in the servervb.config file, we need to store the DemoVB DLL binary in the server’s \bin\Debug project folder so that the server program can find it. Copy all file under the DemoVB\bin\Debug folder. Take note that the DemoVB DLL is a Debug version.

 

Creating the Server VB .NET Console Application: copying the generated DLL files

 

Paste the copied files into the ServerVB\bin\Debug folder.

 

Creating the Server VB .NET Console Application: pasting the copied DLL files

 

Then, we are ready to run this server program. Run the project without debugging. Unblock the firewall if any.

 

Creating the Server VB .NET Console Application: unblocking the Windows firewall

 

The following screenshot shows the server program is ready for the client communication with remoting call.

 

Creating the Server VB .NET Console Application: a sample of server output for remoting application

 

Creating the VB .NET Remoting Client Program

 

Create a new VB .NET console application. You can use the solution and project name as shown in the following screenshot.

 

Creating the VB .NET Remoting Client Program: creating new console application

 

Rename the default given source file to Clientvb.cs to reflect the application to be developed.

 

Creating the VB .NET Remoting Client Program: renaming the source file

 

Add a reference to the previously created DemoVB DLL. By adding a reference from the project in the Visual Studio, the DemoVB DLL and related files automatically copied to the ClientVB \bin\Debug folder. When we rebuild the DLL project, the new updated DLL will also be updated when we rebuild the client program.

Invoke the Add Reference page.

 

Creating the VB .NET Remoting Client Program: invoking the Add Reference page

 

From the Browse page, find and select the DemoVB.dll which is under the \bin\Debug project’s folder. Click OK to add the reference.

 

Creating the VB .NET Remoting Client Program: selecting the DLL file

 

The reference should be in the References page of the project property.

 

Creating the VB .NET Remoting Client Program: invoking the project property page

 

Creating the VB .NET Remoting Client Program: viewing the added reference component

 

Add/edit the code as given below. This client program reads the config file for communication setup and provides two inputs: an integer (int) and a message (string).

 

Imports System

Imports System.Runtime.Remoting

Imports DemoVB

 

' MyClientVB

Class MyClientVB

 

    ' MyClientVB is a simple .Net remoting application that demonstrates

    ' how to configure .Net remoting via configuration files and also shows

    ' how to perform both marshal by reference and marshal by value.

    <STAThread()> _

        Shared Sub Main()

        Console.WriteLine("Client: Reading the config file...")

        RemotingConfiguration.Configure("..\..\clientvb.config", False)

 

        Console.WriteLine("Client: Instantiating the DemoClass object...")

        Dim MyDemo As DemoClass = New DemoClass

 

        ' The following call demonstrates marshal by reference

        ' because we set a numeric value on the server from the client.

        Console.WriteLine("Client: SetValue(77)...")

        MyDemo.SetValue(77)

 

        ' The following call also demonstrates marshal by reference

        ' because we set a string value on the server from the client.

        MyDemo.SetMessage("Client: The message - .Net remoting is cool!")

 

        ' The following call also demonstrates marshal by value because

        ' we retrieve a serializable object named InformationBucket

        ' from the server and get a copy to access on the client.

        Console.WriteLine("Client: Instantiating the InformationBucket object...")

        Dim IB As InformationBucket = MyDemo.GetCurrentInformation()

 

        Console.WriteLine("The message is: " + IB.Message)

        Console.WriteLine("The value is: " + IB.Value.ToString())

    End Sub

End Class

 

Next, we are ready to build this client program. Build the program.

 

Creating the VB .NET Remoting Client Program: building the project

 

------ Build started: Project: ClientVB, Configuration: Debug Any CPU ------

c:\WINDOWS\Microsoft.NET\Framework\v3.5\Vbc.exe /noconfig /imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Generic,

System.Data,System.Diagnostics,System.Linq,System.Xml.Linq /optioncompare:Binary /optionexplicit+ /optionstrict:custom /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /optioninfer+ /rootnamespace:ClientVB /doc:obj\Debug\ClientVB.xml /define:"CONFIG=\"Debug\",DEBUG=-1,TRACE=-1,_MyType=\"Console\",PLATFORM=\"AnyCPU\"" /reference:..\..\DemoVB\DemoVB\bin\Debug\DemoVB.dll,"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll","C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll",

c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll,

c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll,

c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll,

c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll,"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll" /main:ClientVB.Clientvb /debug+ /debug:full /filealign:512 /out:obj\Debug\ClientVB.exe /resource:obj\Debug\ClientVB.Resources.resources /target:exe Clientvb.vb "My Project\AssemblyInfo.vb" "My Project\Application.Designer.vb" "My Project\Resources.Designer.vb" "My Project\Settings.Designer.vb"

vbc : error BC30420: 'Sub Main' was not found in 'ClientVB.Clientvb'.

========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

Again, the Sub Main crap error still happens. Invoke the project property page. Change the Startup object: to Sub Main. The previous error should be resolved when we do the re-building.

 

Creating the VB .NET Remoting Client Program: changing the startup object to Sub Main()

 

Next, add the config file for the client program. Invoke the Add New Item page.

 

 

 

 

Creating the VB .NET Remoting Client Program: invoking the Add New item page

 

Select the Web Configuration File template and you can use the name as in the following screenshot if you want.

 

Creating the VB .NET Remoting Client Program: adding the Web Configuration File to existing project

 

Add the following schema settings at the end of the file, just before the </configuration> tag. The detail of configuration schemas can be found in Remoting Settings Schema.

 

  <system.runtime.remoting>

    <application name="DemoClient">

      <client url="tcp://localhost:4444">

        <activated type="DemoVB.DemoClass, DemoVB" />

      </client>

    </application>

  </system.runtime.remoting>

 

Creating the VB .NET Remoting Client Program: the application configuration file content

 

Then, we are ready to build and run this client program. Build the program and make sure there is no error.

 

Creating the VB .NET Remoting Client Program: building the project

 

Run the program without debugging. The following shows a sample output with exception thrown because there is no server socket listening at the moment when the client program run. Well, it is OK.

 

Creating the VB .NET Remoting Client Program: a sample of console output for the client with thrown exception

 

Testing the Whole .NET Remoting VB .NET Program Sample

 

Next let test the whole .NET remoting project. Open both ClientVB and ServerVB projects in two different Visual Studio instances. Run the ServerVB project and leave the console output.

 

Testing the Whole .NET Remoting VB .NET Program Sample: running the remoting server program

 

Then, from another Visual Studio instance, open and run the ClientVB project.

 

Testing the Whole .NET Remoting VB .NET Program Sample: running the remoting client program, sample output

 

The following screenshot shows the previous server console output.

 

Testing the Whole .NET Remoting VB .NET Program Sample: the previous server console output when communication completed

 


< C# .NET Remoting Program Example | Main | The C++/CLI .NET Remoting Program Example >