Managing errors while loading App.config

  • Thread starter Thread starter Futronics Developer
  • Start date Start date
F

Futronics Developer

I am using 'Settings' (the auto-generated Properties.Settings) and
ConfigurationSection derived classes in my application.

If my configuration file (app.config) gets corrupted (or the user changes it
incorrectly) then I would like my application to pick up settings defaults
and not to keep trying to load from the config file, and hence create an
exception, each time.

Both the auto-generated Properties.Settings and ConfigurationSection derived
classes have defaults. What I'm currently getting is an exception every time
I reference one of these settings if the config file is corrupted.

If the config file is corrupted I would like to somehow set the
ConfigurationManager to an empty or null app.config file so that all
subsequent configuration settings calls will return defaults and won't keep
generating exceptions each time.

The application runs fine on defaults and is much better to do this than to
just terminate.

Does anyone know if this is possible?
 
I don't know if this is possible.

I'd venture a guess that if the configuration file is corrupted should the
application bomb and fall over dead so that the support technician can come
and fix the problem? That should be the driving reason UNLESS the
application must be fault tolerant. If fault tolerance is your goal, you
can't use code to work around this situation because the config file occurs
right after the appdomain for the application is loaded so I suppose its
worth a try to wire the unhandled exception at the app domain level. You'll
still have a problem though because all threads have started to unwind from
the appdomain when that event fires and I'm not sure how you can 'reload'
them.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
 
I'm not sure if I understood your problem correctly, but you can trap
app.config/settings.settings errors if you try to reference a setting from
your Main() like this:
static void Main(string[] args)
{
try
{
int expectedOk=Settings2.Default.setting2;
}
catch (ConfigurationErrorsException ex)
{
// we have a problem here
// recreate configuration
// then re-read
ConfigurationManager.OpenExeConfiguration("");
}
}

I think you could also setup an unexpected error handler that resolves the
problem and then restarts the app.



"Futronics Developer" <[email protected]> ha
scritto nel messaggio
news:[email protected]...
 
Back
Top