The new model provided by .NET smart clients has really taken hold in the imaginations and the business plans of .NET developers. The idea of getting the best of both the richness of real Windows applications and the zero-touch installation of Web-based applications is turning the heads of many an IT professional. Of course, as soon as a brand new model starts being put to real use, problems show themselves.
One common problem that happens fairly quickly is the need to distribute a .config file along with a smart client. The .config file is a handy way to re-configure an application without re-compiling. Imagine a .config file as simple as this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="teststring" value="My Test String" />
</appSettings>
</configuration>
Associating a .config file with an application requires that it be properly named; i.e., VbSmartClient.exe would have a .config file named VbSmartClient.exe.config, placed in the same directory as the application, as shown in Figure 1.
![]() |
| Figure 1: An application and its .config file |
Given an associated .config file, one way for an application to access it is
using the AppSettingsReader from the System.Configuration class:
Dim settings As New System.Configuration.AppSettingsReader()
Dim o As Object = settings.GetValue("teststring", GetType(String))
MessageBox.Show(o.ToString())
Assuming the .config file in the right place, formatted in the right way, running this code yields the message box shown in Figure 2.
![]() |
Figure 2: A string from the appSettings section of the .config file |
The AppSettingsReader class reaches into the application's .config file and
parses the <appSettings> element to provide name/value pairs made available
with the GetValue method. It gets the name of the file to read from the application
domain data, specifically the APP_CONFIG_FILE data returned from the GetData
method of the current AppDomain:
MessageBox.Show(System.AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE"))
The APP_CONFIG_FILE data will contain a path to the .config file relative to
the application base (available by passing “APPBASE” to GetData)
and based on the name of the .exe itself, e.g., VbSmartClient.exe.config.
While the .config file will always be based on the name of the application,
the actual path to the .config file won't always be to a file on your
hard drive. In the case of a smart client, i.e. a WinForms application launched
via an URL, the .config file will be accessed via an URL, because the APPBASE
will be the URL from which the application was downloaded. To test this, I right-clicked
on the bin directory created by VB.NET that contained the .exe and the .config
files for my smart client and chose Properties and then the Web Sharing tab.
I named the web share "ConfigFun," and tested smart client launching with the following
URL:
http://localhost/ConfigFun/VbSmartClient.exe
Unfortunately, this caused my application to fail, as shown in Figure 3.
![]() |
| Figure 3: .config not available by default for smart clients |
This happened because, by default, ASP.NET disables download of any kind of source file from your web site, including any .config files. This is good, because it protects your web.config file, but bad because it disables your application's .config file from being downloaded. To enable this, you need the following minimal web.config file at the root of your web share folder:
<configuration>
<system.web>
<!--
Remove the *.config handler so that we_can serve up
*.exe.config files, but make it forbidden to serve up the
web.config file itself.
-->
<httpHandlers>
<remove verb="*" path="*.config" />
<add verb="*" path="web.config"
type="System.Web.HttpForbiddenHandler"/>
</httpHandlers>
</system.web>
</configuration>
This web.config configures your ASP.NET web application to remove the restriction on downloading .config files, but to enable the restriction for web.config files specifically. This lets your application's .config file through, but stops your web.config file from being available to any casual observer.
Unfortunately, that darn message box still hasn't gone away. One more step is needed. Out of the box, IIS doesn't allow .config files to be downloaded unless anonymous access is enabled. So to enable anonymous access for the ConfigFun web share, choose Start->Programs->Administration Tools->Internet Information Services, find the ConfigFun web share, right-click and choose Properties, choose the Directory Security tab and enable Anonymous access, as shown in Figure 4.
![]() |
| Figure 4: Enabling anonymous access for a web share |
Once you've done this last tweak, you'll all set. The .config file
will be downloaded along with your application and your AppSettingsReader will
be happy.
Three things are needed to download the .config file with the application:
APPBASE, aka folder, as the .exe it's
meant to configure.Once this is done, as the .config file changes, the updated .config file will be downloaded and cached automatically, letting you use the .config file as a way to configure applications on user's machines without manually deploying configuration information, just as a smart client itself is automatically deployed.
Chris Sells is a Microsoft Software Legend and a Program Manager with the Distributed Systems Group at Microsoft. His weblog at http://www.sellsbrothers.com is popular with .NET developers for its zany and independent commentary on technology and geek culture.
Return to ONDotnet.com
Copyright © 2009 O'Reilly Media, Inc.