1. Sample Application for this Config File Example
The config file in dot.net contains application related settings. This file is useful to make application related settings at run-time and there by we can avoid re-compiling it. In this article, we will try how to use the ‘Config File’ on the server and client side of the remoting applications. Instead of building up the example from scratch, we will take one of our past examples and make use of the config file in it. The early article example we will choose is Server Activated, Singleton.
2. Server Side Remoting Config File
2.1 Creating the Config File
To add a Config File to the server project, we should right-click the project name in the Solution Explorer, then select ‘add item’ from the context menu. From the open dialog, we chose the Application Configuration File as shown in the below picture.
In the Solution Explorer, you can now see a file named app.config. We can double click the file to open it. Then we will 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 Config File is the better alternative for the programmable configuration that already exists in the server project. So, we need to comment them. We comment the following code in the ‘program.cs’ file as we will use config settings.
//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);
2.2 Server Config Details
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 is replaced by the Config File’s Channel Section. In the Config File, we 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 code snippet, ‘Server 007’. We commented this also as we have it in the Config File under the Service Section.
Now look at the code below, which loads the remote server using the Config File shown above. Note that the Config 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, we see that build operation deployed Config File also with a different name in place of default app.config. Even the name is different, the content is same. So, we referred this deployed file name in the call for RemotingConfiguration.Configure().
//Server 006,007: Host the Server from Config File RemotingConfiguration.Configure("SingletonServer.exe.config", false);
We can now launch the server application as before. The server takes all the setting from the config file.
3. Client Side Remoting Config File
It does not require much explanation 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>
The commented code and newly added code are 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 formerly we used the ‘Activator.GetObject’ to get remote proxy and in the commented code we get the required object after specifying the remote object, transmission channel and port number. And now we commented that as we have it in the Config File. After loading the Config File, we can create the object using the new operator. This will create the object on the server, and we have a proxy here in the client for making the method calls on the remote object.
4. Avoid Mistakes While Using Remoting Config
If you make mistakes in the Config File, we will not get the compilation errors as most of the configurable parts are tied to the run-time. In that case, creating the object using the new RegVotes() will create the object in client machine with no proxy to server. This means there is no object on the server.
One can use the below picture to know the union between client and server. Making a mistake will give strain later.
Source Code: Download From Google Drive