CodeSnip: Using ConfigurationManager.RefreshSection method in .NET 2.0
page 1 of 1
Published: 25 Apr 2006
Unedited - Community Contributed
Abstract
In this article, Steve shows how to update an application config file and have the new settings take effect without stopping and starting a Windows .NET service.
by Web Team at ORCS Web
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 15465/ 18

This application uses the System.IO.FileSystemWatcher class in .NET 2.0.  This was not easy in .NET 1.0 or .NET 1.1, but only requires a couple lines of code in .NET 2.0. 

My "hello world" application monitors a website on a periodic basis and records the results to the Event Log, Database or sends an email.  You can download the code to see how I implemented this in my application.  I have included the Setup project to install the application. 

Here is the code that refreshes the 'appSettings' section.

Listing 1

protected void fsw_Changed(object sender,System.IO.FileSystemEventArgs e)
 {

  System.Diagnostics.EventLog.WriteEntry
("Application","FileChanged:" + e.Name); 
 ConfigurationManager.RefreshSection("appSettings"); 
  LoadConfig(); } 

To test this out, install the application on your machine and start the 'AWebMonitorServiceCS' service.  After the service is running, change the URL setting to a different URL.  After so many seconds, that is configured in the 'Interval' appSetting and this will record the data in the Event Log.   There will also be entries in the event log showing the 'awebmonitorserviceCS.exe.config' has changed. 

Sample event log text when the file changed

The description for Event ID ( 0 ) in Source ( Application ) cannot be found.

The local computer may not have the necessary registry information or message

DLL files to display messages from a remote computer. You may be able to use

the /AUXSOURCE= flag to retrieve this description; see Help and Support for details.

The following information is part of the event:

FileChanged:AWebMonitorServiceCS.exe.config.

Sample event log text what URL is being monitored

The description for Event ID ( 0 ) in Source ( Application ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer.

You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details.

The following information is part of the event:

http://www.aspdot.net

Downloads

[Download Sample]

Summary

I hope you find this code snippet useful.  With a couple of lines of code it can enhance your Windows service application.

 



User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-05-27 11:40:31 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search