Adding a Configuration File


This may seem like a strange heading to place in this chapter, but it is not. I have already demonstrated the use of an application configuration file with the Internet Information Server (IIS) remoting objects. You have seen that you can store application-specific parameters and retrieve them quite easily. So why would you want to create yet another configuration file for your local objects? How often have you had to create one build for test and another for development and yet another for a demonstration environment? Or how many times have you had to change the code if a server location changed or any number of other reasons? The answer is probably a lot.

The addition of a configuration file is just as simple and straightforward as it is for the web.config file. Your configuration file for the local objects is going to store the Uniform Resource Locator (URL) of your remote objects. To begin with, add a configuration file to the NorthwindTraders project. To do this, select Project Add New Item and select the Application Configuration File entry. Leave the filename as App.config. Edit the App.config file so that it looks identical to the Extensible Markup Language (XML) in Listing 8-6.

Listing 8-6: The App.config File

start example
 <?xml version="1.0" encoding="utf-8" ?> <configuration>   <appSettings>     <add key="Northwind_IIS"      value="http://localhost:80/Northwind/"/>   </appSettings> </configuration> 
end example

Now you need to edit the code in the NorthwindUC AppConstants class. The code change is quite simple, but before you edit the code, import the System.Configuration namespace into the heading of the code module. The original code is as follows:

 Public Class AppConstants      Public Shared REMOTEOBJECTS As String = "http://localhost:80/Northwind/" End Class 

Edit the code so that it looks like the following:

 Imports System.Configuration Public Class AppConstants      Public Shared REMOTEOBJECTS As String = _      ConfigurationSettings.AppSettings("Northwind_IIS") End Class 

You are done. But you might be asking yourself one question now: "Why did you add an application configuration file to the NorthwindTraders project instead of the NorthwindUC project?" Well, the answer is that the NorthwindTraders project is the startup project and therefore it is the application, not the NorthwindUC assembly.

Each application can have only one application configuration file, which is why you did not rename it. However, there is also a different reason you did not rename it—the file is renamed when it is deployed. To see this, run the application and then check the bin folder of the NorthwindTraders project. You will now see a configuration file called NorthwindTraders.exe.config. By default, configuration files are renamed to the application executable file name plus the .config extension.

With this change you can now change the location of your IIS server and your data server without recompiling and redistributing the application. All you have to do is send out an updated configuration file—it is much simpler now!




Building Client/Server Applications with VB. NET(c) An Example-Driven Approach
Building Client/Server Applications Under VB .NET: An Example-Driven Approach
ISBN: 1590590708
EAN: 2147483647
Year: 2005
Pages: 148
Authors: Jeff Levinson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net