views:

343

answers:

3

I am looking at ways to make our application more extensible and easier to manipulate without having to alter the web.config (or, in our case, application.config files, which contain the appsettings node).

One way I have thought about is keeping the app settings in the database table that has a sqlcachedependancy. This means that:

  • Any time a setting is changed in the database, the cache is invalidated, and the settings are retrieved again, thus updating the application in realtime without having to alter files and restart the entire app.
  • We can create a custom tool which allows us to alter the settings.

The cons as I see it are that this may cause serious logic problems in that, if you have something that checks an appsetting at the start of a process, and it then changes halfway through, you could end up unintentionally altering the process flow, as the requirement for a complete application restart is negated.

Is there a way round this?

Is there a better way to manage appsettings, so that you can alter them on the fly remotely for one, several, or all servers in one go?

+1  A: 

I think you've nailed the two major players:

  • either you have access to the file system and you put all your settings in a plethora of *.config files there

OR:

  • you don't have access (or only very limited access) to the server's file system and thus you're probably better off putting config settings and user preferences in a database, basically leaving nothing but the connection string to the config file on disk

Both approaches have their pros and cons. I've been trying for a long time to find a way to "materialize" a config section from a database field, so that I could basically just use the config XML, but stored in a database field. Unfortunately, the entire .NET 2.0 config system is very much "locked down" and just only assumes data will come from files - there's no way to plug in e.g. a database provider to allow the config system to read its contents from a database field :-( Really too bad!

The only other approach I've seen is a "ConfigurationService" in the StockTrader 2.0 sample app provided by Microsoft, but for my needs, it felt like overkill and like a really complex, really heavy-weight subsystem.

Marc

marc_s
What I had planned was a sort of 'config façade' where there settings wouldn't really be app settings loaded in, but a separate entity with no XML. This config façade would be strongly-typed, but would be invalidated if the table is changed.
Dan Atkinson
Yes, we have something like that - a "config" class which basically wraps the underlying config table into a strongly typed, easy to use object
marc_s
How did you get around possible conflicts with the logic flow as described in the question?
Dan Atkinson
we don't dynamically update - we can either update manually, on purpose, if we're quite sure it's okay and doesn't disturb anything, or then it'll be updated next time the app pool gets recycled / IIS gets reset.
marc_s
+1  A: 

If you reference an external config file that contains appsettings (leaving everything else in the normal app.config) then I believe editing it only reloads those settings, it doesn't force the whole app to restart.

There's a similar question on the subject here: http://stackoverflow.com/questions/1380240/nested-app-config-web-config-files

WRT the problem of values changing in the middle of program execution, I guess you could locally cache the values, and raise an event when they change, allowing routines to reach a suitable point before using the updated values.

I think in asp.net we sort of get this for free because each page lifecyle is distinct, so the value is simply applied to new page requests only, not in the middle of an execution.

Edit: A little extra info:

Configuration Changes Cause a Restart of the Application Domain

From MSDN:

Changes to configuration settings in Web.config files indirectly cause the application domain to restart. This behavior occurs by design. You can optionally use the configSource attribute to reference external configuration files that do not cause a restart when a change is made. For more information, see configSource in General Attributes Inherited by Section Elements.

More information on the ConfigurationManager class in the System.Configuration namespace which could be used to modify the config files programatically (ie in a custom tool, if relevant disk read permissions can be provided). If you stick to using the built in configuration classes, I think changing the external configs, would not cause application restart, but would raise events (such as property changed) which you could handle, to ensure your code is not caught out by changing settings.

Andrew M
Assuming then that it does, I would still have to edit a file, which I don't want to do. I'd rather have it in some kind of custom tool.
Dan Atkinson
You could write code to update your configs using the CnfigurationManager class, but if ease of changing settings is a requirement then the SQL option would seem more appealing.
Andrew M
+1  A: 

You could use SQLite, which will be a self-contained DB in a single file. Two birds with one stone?

Randolph Potter
I'm already using SQL Server, and I have admin tools which use that database. If I add a new db, it would be a little pointless. Also, I'm not completely au-fait with SQLite.
Dan Atkinson
Fair enough. However, SQLite is small enough to do what you need (I think). There is a LINQ interface for SQLite which makes life easier. http://code.google.com/p/dblinq2007/
Randolph Potter