Wednesday, March 21, 2012

[ Dot Net Remoting ] - Using Configuration (config) files in Server and Client side

1. Introduction

In this article, I will show how to use the "Configuration File" on server and client side of the remoting applications. Instead of developing the application from scratch we will take the already developed one and make use of the configuration file. We take a previous article example (Server Activated, Singleton) and use it here.

You can take that example from download link from this article => Refer

2. Configuration File (Server)

To add a configuration file to the server project, right-click the project name in the solution explorer, then select add item from the context menu. From the displayed dialog, chose the Application configuration file as shown in the below picture.

Adding Dotnet Application Configuration File
Fig 1. Adding Dotnet Application Configuration File


In the solution explorer, you can now see a file named app.config. Double click the file to open it. Then enter the details as shown below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
            <system.runtime.remoting>
                        <application>
                                    <service>
                                                <wellknown mode="Singleton" type="SingletonServer.RegVotes, SingletonServer" objectUri="RegVotes"      />
                                    </service>
                                    <channels>
                                                <channel ref="tcp" port="1334"   />
                                    </channels>                             
                </application>
            </system.runtime.remoting>
</configuration>

Now this configuration file is the better alternative for the programmable configuration that already exists in the server project. So we need to comment them. In the program.cs file the following code is commented out:

//Server 006: Create and Register TCP Channel
//TcpServerChannel SrvrChnl = new TcpServerChannel(13340);
//ChannelServices.RegisterChannel(SrvrChnl, false);

//Server 007: Register the Remote Class so that the Object can go
//and sit on the Remote pool
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(SingletonServer.RegVotes), "RegVotes", WellKnownObjectMode.Singleton);

Note that the commented piece of code uses the TCP channel 13340 and registers the same for accepting the client requests. So the code portion marked as Server 006 tag is replaced by the configuration file’s channel section. In the configuration file, I changed the port to 1334.

Then we registered our remote object using the RegisterWellKnownServiceType function call. In the above code, this is done in the Tag Server 007. We commented this also as we have it in the configuration file under the Service section.

Now look at the code below, that loads the remote server using the configuration file shown above. Note that the configuration file we added is App.config and what we specified to configure function is something different. This is not a magic. Go to the folder in which the build operation deployed the server executable. In the same folder, you will see that build operation deployed configuration file also with a different name app.config. Even the name is different, the content is same. So we referred this deployed file name in the call RemotingConfiguration.Configure

//Server 006,007: Host the Server from configuration file
RemotingConfiguration.Configure("SingletonServer.exe.config", false);

You can now launch the server application as before.

3. Configuration File (Client)

No need to explain much here. Below is the app.config file added in the same way as we did in for the server project:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
            <system.runtime.remoting>
                        <application>
                                    <client>
                                                <wellknown type="SingletonServer.RegVotes, SingletonServer" url="tcp://localhost:1334/RegVotes"  />               
                        </client>
                        </application>     
             </system.runtime.remoting>
</configuration>

And, the commented code + newly added code are shown below:

//Client 003: Get the Proxy for the Remote object from the server
//remobj = (RegVotes)Activator.GetObject(typeof(RegVotes), "tcp://localhost:13340/RegVotes");
RemotingConfiguration.Configure("SingletonClient.exe.config", false);
remobj = new RegVotes();

Note that previously we used the Activator.GetObject. And we commented that as we have it in the configuration file now. However, in the commented code we get the required object after specifying the remote object, communication channel and port number. Now, after loading the configuration file we can create the object using the new operator. This will create the object on the server, and you have a proxy here for making the method calls in the remote object.

4. Careful when using configuration file

If you make mistakes in the configuration file, we will not get compilation error as most of the configurable parts are tied to the runtime. In that case, creating the object using the new RegVotes() will create the object from the available in the client reference itself.

Use the below picture to know the association between client and server. Making a mistake will give trouble later.

A Sample Dot Net Remoting Configuration File (Client & Server)
Fig 2. A Sample Dot Net Remoting Configuration File (Client & Server)

Source Code: Download

No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Firend :)