|
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.
Rename the source file to Demo to reflect the application that will be developed. |
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.
The DLL file should be generated and ready for use.
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.
Rename the source file to Servervb to reflect the application to be developed.
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.
Change the Startup object: to Sub Main. The previous error should be resolved.
Invoke the Add New Item page.
Select the Web Configuration File template and you can use servervb.config as the file name if you want.
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> |
Re-build the project and make sure there is no error else please correct it.
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.
Paste the copied files into the ServerVB\bin\Debug folder.
Then, we are ready to run this server program. Run the project without debugging. Unblock the firewall if any.
The following screenshot shows the server program is ready for the client communication with remoting call.
Create a new VB .NET console application. You can use the solution and project name as shown in the following screenshot.
Rename the default given source file to Clientvb.cs to reflect the application to be developed.
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.
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.
|
|
The reference should be in the References page of the project property.
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.
------ 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.
Next, add the config file for the client program. Invoke 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.
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> |
Then, we are ready to build and run this client program. Build the program and make sure there is no error.
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.
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.
Then, from another Visual Studio instance, open and run the ClientVB project.
The following screenshot shows the previous server console output.