moving the config files for a dll to the app that calls the dll

前端 未结 3 1533
逝去的感伤
逝去的感伤 2020-12-06 19:30

I have a web app that has search functionality. The search algorithm is compiled to a separate dll. In the C# code for the search algorithm, I am using strings held in a s

相关标签:
3条回答
  • 2020-12-06 20:06

    Your best bet is to add the config to the web project directly. .NET doesn't really support configuration associated with a library in the way you are attempting; and this is by design. Other users of your library may need different configuration. Mark the file as being content that should be copied to the output folder.

    EDIT:

    To do this set "Build Action" to "Content" and "Copy to Output Directory" as "Copy if newer" in the file properties. You can discover the file in the bin folder using HttpRuntime.BinDirectory. You may want to pass this location to your library rather than have the library assume it's running in a web project.

    Alternatively, just embed the config you need in web.config (config files have a facility to break out settings into a separate file as well).

    Finally, you could consider embedding the file as a resource.

    EDIT:

    Looking at the code you are using, just move it into web.config. Much easier, and idiomatic. Once it's in web.config just use ConfigurationManager.GetSection() to read your section.

    Incidentally, there's a free configuration section designer that makes creating the classes to support custom configuration sections very easy. Look in the VS Extensions Online Gallery here:

    http://visualstudiogallery.msdn.microsoft.com/2a69f74e-83df-4eb0-8cac-cd83b451cd1d?SRC=VSIDE

    0 讨论(0)
  • 2020-12-06 20:08

    The ideal way is to remove the configuration dependency from DLLs. A dll is designed to be used by an application and configuration belongs to application not a dll.

    In most of the scenarios, you can get rid of depending on/reading from config in dll code by using dependency injection either through DI container or through manual compose.

    if your search dll depend on the settings, make the settings as a dependency for your dll entry point class and proceed with dll code assuming somehow your entry point class gets it's settings.

    Then, you can supply the settings value from your application, be it web/windows/console and by reading from configuration file/db/web service/file system.

    sample code:

    In dll:

     public interface ISearcherDirectorySettings
    {
        string[] SearchIndexPointers { get; }
    }
    
    public class Searcher
    {
        private readonly ISearcherDirectorySettings _searchDirctorySettings;
    
        public Searcher(ISearcherDirectorySettings searchDirtorySettings)
        {
            _searchDirctorySettings = searchDirtorySettings;
        }
    
        public void SearchAlgorithm()
        {
            var indexes = _searchDirctorySettings.SearchIndexPointers;
            // search code
        }
    }
    

    In your application:

    public class SearcherDirectorySettings : ISearcherDirectorySettings
    {
        private readonly string[] _pointers;
        public SearcherDirectorySettings(string[] pointers)
        {
            _pointers = pointers;
        }
    
        public string[] SearchIndexPointers
        {
            get { return _pointers; }
        }
    }
    
    public class ApplicationRootClass //Owns configuration file
    {
        const string FirstPointerKey = "File1";
        const string SecondPointerKey = "File2";
    
        private Func<string, string> _getFromConfig = key => ConfigurationManager.AppSettings[key];
    
        public ApplicationRootClass()
        {
            var searcherDirectorySettings = new SearcherDirectorySettings(new[] { _getFromConfig(FirstPointerKey),_getFromConfig(SecondPointerKey) });
    
            var searcher = new Searcher(searcherDirectorySettings);
            searcher.SearchAlgorithm();
        }
    }
    

    With this, you can achieve "fail fast". you can have the search dlls used in any application and it will be the responsibility of the application to supply settings value.

    If you end up using the dll in multiple applications/projects and duplicating the settings class code, have a utility component that does the job or move the settings class into the dll but leave the instantiation of settings class to the application.

    0 讨论(0)
  • 2020-12-06 20:10

    Here's how you do what you want to do, as opposed to what some think you should do:

    (sorry I'm working in vb at the moment)

    Say the dll is named mycompany.mynamespace.dll

    1. Rename the app.config file in your dll project to mycompany.mynamespace.dll.config
    2. In its properties, say Copy If Newer
    3. Use this code (within the dll) to access your setting:

          Dim appConfig = ConfigurationManager.OpenExeConfiguration(Me.GetType.Assembly.Location)
          _WorkingDirectory = appConfig.AppSettings.Settings("WorkingDirectory").Value
      
    0 讨论(0)
提交回复
热议问题