If an ASP.NET application uses multiple DLLs, what is the best approach to store the configurations of each dll library?

After creating the settings for each dllfile is created .dll.config. If this dllis part of an asp.net application, how to save these configurations separately for each dlland not merge them into web.config?

Example: I have a GMailSender class library (DLL) that sends email through GMail servers. All you need is an instance of GMailSender, for example:

GMailSender gms = new GMailSender();
gms.To = "myfriend@yahoo.com";
gms.Subject = "System.Configuration dilemma";
gms.Body = "Can anyone help me with this question ?";
gms.Send();

Let's look GMailSenderinside GMailSender.dll, and the configuration file GMailSender.dll.config, and the username and password of the GMail account are inside it.

I want this one to dlluse its own configuration file (both dll and config in the same directory, i.e. in the BinASP.NET application folder ) or next to the desktop application. Thus, GMailSender is independent of who uses it to retrieve its configurations (the current AppDomainone that loaded this one dll).

I want this without wheel repair (without custom configuration classes). I assume this is possible with System.Configuration, but this namespace is probably the worst of .NET!

Please do not say why you are designed to ...

plug-in, , , MEF .NET 4.0, Parts. MEF plugin-based.

+3
5

, . MSDN , .

key/value, appSettings, .

, , web.config:

<configSections>
  <section name="GmailSettings" restartOnExternalChanges="true" type="System.Configuration.NameValueFileSectionHandler" />
</configSections>

web.config GmailSettings , :

<GmailSettings configSource="GmailSettings.config"></GmailSettings>

configSource , GmailSettings. restartOnExternalChanges, . true, , GmailSettings.config( web.config).

, GmailSettings.config:

<?xml version="1.0"?>
<GmailSettings>
  <add key="userName" value="blabla"/>
  <add key="password" value="moreBla"/>
</GmailSettings>

GmailSettings ConfigurationManager.GetSection() , :

public class GmailSettings
{
    private static readonly GmailSettings _instance = new GmailSettings();
    private NameValueCollection _settings = ConfigurationManager.GetSection("GmailSettings") as NameValueCollection;

    public static GmailSettings Instance
    {
        get { return _instance; }
    }

    public string this[string key]
    {
        get { return _settings[key]; }
    }
}

, , GmailSettings.Instance["userName"].

, .

+4

App.config DLL . . DLL .

DLL , DLL- .dll.config, App.config/Web.config( ASP.NET), .

.config, "Mydll.dll" . , DLL-.

, .

+1

, , , , - DLL , , [1] - .

static class DllConfigurationFactory
{
    public static Configuration GetConfigurationInstance()
    {
        string           callingAssemblyPath = Assembly.GetCallingAssembly().Location ;
        string           configFilePath      = string.Concat( callingAssemblyPath , ".config" ) ;

        // getAttributes hurls with a multitude of exceptions
        FileAttributes   attributes  = File.GetAttributes( configFilePath ) ;
        bool             isDirectory = ( (attributes&FileAttributes.Directory) == FileAttributes.Directory ? true : false ) ;
        if ( isDirectory )
        {
            throw new FileNotFoundException() ;
        }

        // check for (and die with an SecurityException) if we dont' have read access
        new FileIOPermission( FileIOPermissionAccess.Read , configFilePath ).Demand() ;

        ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
        configMap.ExeConfigFilename = configFilePath ;
        Configuration instance = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
        return instance ;
    }

}

factory :

  • (( , factory - , , DLL).

  • ".config" ( "C:\usr/bin/local/foo/myAssembly.dll.config".

  • , , , .

  • , System.Configuration.Configuration .

  • Visual Studio app.config DLL foo.dll.config, , // (). Visual Studio bin DLL, .
  • , - , "" , . . , , . YMMV .

!

[1] TMTOWTDI, Perl ( ). machine.config.

, .Net: factory XML . , , . ... Configuration, .

+1

, , . , ASP.NET DLL, . DLL Bin. THey appDomainSetup.CachePath, *.dll.config .

refrence *.dll.config web.config / / DLL web.config; File web.config.

, , .

+1

web.config :

ConfigurationManager.AppSettings ["DllName.MySetting"]

This means that all configuration settings are in one place, and not in the Bin folder.

0
source

Source: https://habr.com/ru/post/1772394/


All Articles