开发者

Is there a way to ensure a class library uses it's own app settings?

I have a class library at the moment and it's going to need its own appsettings and it's likely going to need to change them and stuff.

The only problem is that the class is going to be called by a web application which has an appsettings file that also has identical keys to the class library app settings.

I would like to ensure that the class library uses it's own appsettings file and also that it doesn't make changes to the web application's appsettings.

Is this possible? It seems like any assembly can change the appsettings via ConfigurationManager.

EDIT - To give context, the class library accesses a data tier assembly (which I can't change) which uses a value in the appSettings to get the connection string. Only problem is that key is the same as the web app's connection string (which may or may not have a different value).

I don't want the class libr开发者_StackOverflow中文版ary to change the appSetting value for that key, and then have the web app start accessing a different database.


A separate app domain can use a different config file, I believe. I'm not sure how else you could do it.


There is no way to do this. It simply isn't how .NET works.

Consider that the class library could be used in two separate applications. Each application might need different settings for the same class library. The only way this works is if one does not use the config file from the class library.


You can use ConfigurationManager.OpenMappedExeConfiguration to get the configuration for a specific file from within your class library.

 // Map the new configuration file.
 var configFileMap = new ExeConfigurationFileMap();
 configFileMap.ExeConfigFilename = configFile;

 // Get the mapped configuration file
 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
    configFileMap, ConfigurationUserLevel.None);

Here config will give you access to AppSettings, ConnectionStrings and Sections. You could create an abstraction on top of the access to the ConfigurationManager so you can control its access and thus make it more testable but that depends on the design.

As you mentioned auto generated code in one of your answers there are sometimes ways around that. Most likely that class is marked as partial which you might be able to initialize with the correct 'Configuration' object and therefor the correct connection string


Instead of using the app/web.config file consider building your own settings class that inherits from System.Configuration.SettingsBase.

Your class library can then instantiate a copy of this SettingsBase implementation and manage its own settings without interferes with settings that might be required elsewhere.

http://msdn.microsoft.com/en-us/library/system.configuration.settingsbase.aspx

The settings class would look something like this:

sealed class MySettings : SettingsBase
{
    [ApplicationScopedSetting()]
    [DefaultSettingValue("Default")]
    public string MySetting
    {
        get { return this["MySetting"].ToString(); }
        set { this["MySetting"] = value; }
    }
}


Name your config file like below and place it in the same folder as your assembly: [AssemblyName].dll.config

Update: Anything that goes under can be used. You also use AppSettingsReader (http://msdn.microsoft.com/en-us/library/system.configuration.appsettingsreader.aspx) to create custom settings.

ex:

<configuration>
  <appSettings>
    <add key="YourCustomSetting" value="10240" />
  </appSettings>

  <system.diagnostics>
    ...
  </system.diagnostics>

  <connectionStrings>...
  <system.web>...
  etc, etc.
</configuration>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜