Remoting Configuration using VB.NET: Part 1

In this article I will explain you about VB.NET Remoting Configuration.
  • 1543

Remoting configuration files, like other configuration files in VB.NET, are Extensible Markup Language (XML) files that use the .config extension. Configuration files can be used in remoting in lieu of source code creation and registration. In the examples so far, there hasn't been a desperate need for configuration files, but as we get into customized sinks, the amount and complexity of code required to create and register remote objects increases dramatically. Configuration files also permit configuration changes without code changes.
 
 The naming convention for remoting configuration files is the executable program name and extension, with a .config extension. Thus, for ClientActivatedServerExe.exe, the configuration file would be named ClientActivatedServerExe.exe.config. At this point, it should be evident that less wordy project names are a good idea! The location of the configuration file must be in the same directory as the executable file.
 
 The configuration files are located in the Samples1 with the source files in the directories of the four executable files. They should be moved to the respective "bin\debug" directories.
 
 Listing 25.19 is an example of the configuration file for ClientActivatedServerExe. The XML declaration tag <?xml version="1.0 ?>, although not required by the XML specification, will be used in all examples, because of convention.
 
 Listing 25.19: ClientActivatedServerExe.exe.config

 
 <?
xml version="1.0" ?>
 <
configuration>
           <
system.runtime.remoting>
                    <
application name="Simple">
                              <
service>
                                       <
activated type="SimpleObjectLib.SimpleObject, SimpleObjectLib"/>
                              </
service>
                              <
channels>
                                       <
channel ref="TCP Server" name="Client Activated Tcp Server" port="1234"/>
                                       </
channel>
                              </
channels>
                    </
application>
           </
system.runtime.remoting>
 </
configuration>
 
 This is the server's configuration file, and like all VB.NET configuration files, the root element is <configuration>. Remoting configuration is placed in the subelement <system.runtime.remoting>. This is a configuration for a client-activated object. The <application> tag contains the optional name attribute that identifies the URI of the remote object. If we dropped the name attribute, the client's configuration tag, below, would have to be changed to <client url="tcp://localhost:1234">.
 
 The two subelements of <application> are <service> and <channels>. An optional subelement is a <lifetime> tag. This defines the leasing lifetime of all remote objects on the server.
 
 The <service> tag contains information about the remote objects and is a server-specific element. The <channels> tag is common to both client and server configuration and contains information about the channels to be used by the remote object. Both of these elements can encapsulate multiple remote objects and channels.
 
 The <activated> element identifies the remote object as being client activated. The type attribute gives the remote object's name and assembly. Two optional attributes deal with leasing, leasetime and renewoncall. These can be used to override the default time associated with those leasing properties.
 
 The <channel> tag on the server contains two attributes. The type attribute, as with the other type attributes discussed so far, names the channel type and the assembly to be used. The port attribute indicates the port the channel will listen on.
 
 Listing 25.20: ClientActivatedClientExe.exe.config.

 
 <?
xml version="1.0" ?>
 <
configuration>
           <
system.runtime.remoting>
                    <
application name="ClientActivatedClientExe">
                              <
client url="tcp://localhost:1234/Simple">
                                       <
activated
                                      
type="SimpleObjectLib.SimpleObject, SimpleObjectLib"/>
                              </
client>
                    </
application>
           </
system.runtime.remoting>
 </
configuration>
 
 The client configuration file (see Listing 25.20) is very similar to the server's. The primary difference is the server's <service> tag counterpart, the <client> tag. The <client> element contains the url attribute, which, as always, identifies the location of the remote object. Using configuration files in source code is straightforward.
 
 Listing 25.21: ClientActivatedServer.vb

 
       
Private Shared Sub Main(ByVal args As String())
         Dim file As String = "ClientActivatedServerExe.exe.config"
 
        RemotingConfiguration.Configure(file)
     End Sub

 
 Simply call RemotingConfiguration.Configure() with the name of the configuration file as a parameter (see Listing 25.21). Remember that the configuration file must be in the same directory as the executable file, or the path must be included in the configuration file's name parameter. To see that the channel has been created and that channel and remote object have been registered, you can use the RegisteredChannels property from ChannelServices and the RemotingConfiguration's GetRegisteredActivatedServiceTypes() method.
 
 The server-activated example (see Listing 25.22) demonstrates another facet of remoting configuration files, the template.
 
 Listing 25.22: ServerActivatedServerExe.exe.config

 
 <?
xml version="1.0" ?>
 <
configuration>
           <
system.runtime.remoting>
                    <
channels>
                              <
channel id="Http Server"
                             
name="Server Activated"
                             
type="System.Runtime.Remoting.Channels.Http.HttpServerChannel, System.Runtime.Remoting"
                             
port="1234"/>
                    </
channels>
                    <
application name="ServerActivatedServerExe">
                              <
service>
                                        <
wellknown
                                      
mode="SingleCall"
                                      
type="SimpleObjectLib.SimpleObject, SimpleObjectLib"
                                      
objectUri="Simple">
                                       </
wellknown>
                              </
service>
                              <
channels>
                                       <
channel ref="Http Server"/>
                              </
channels>
                    </
application>
           </
system.runtime.remoting>
 </
configuration>
 
 Conclusion
 

Hope this article would have helped you in understanding VB.NET Remoting Configuration. Remaining part of this article you will see in my next article.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.