.net – How to enable user settings to libraries used by a web project hosted WCF service


By default a IIS hosted WCF service can't use libraries that uses user scope settings. The only scope that it accepts is the Application scope.

When you try to do otherwise it throws an exception:

[System.Configuration.ConfigurationErrorsException]{"The current configuration system does not support user-scoped settings."}

How to circumvent this?

Best Solution

You could use the .NET 2.0 config system to load/manage a user-scoped config file for your service, e.g. located in IsolatedStorage or something.

Once you have that file, you can load and access its contents with code something like this:

ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "ConfigLibrary.config";  // set it to whatever 

Configuration libConfig = ConfigurationManager.
                  OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

AppSettingsSection section = 
                   (libConfig.GetSection("appSettings") as AppSettingsSection);

string value = section.Settings["Test"].Value;
string item = section.Settings["Item"].Value;

and so on. This uses the standard, default .NET 2.0 config stuff, but it allows you to have your own custom config files, named whatever you like, located wherever you like, and they can be accessed directly from your class library, no need to put them into web.config or your host app's app.config.

For more on the .NET 2.0 config system, check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

Highly recommended, well written and extremely helpful!