Single config file for solution

后端 未结 3 2081
旧时难觅i
旧时难觅i 2021-01-04 14:03

Now I have seen this question before on SO in a variant ways, but surprisingly not in this form:

I have a solution with multiple web services (projects) that need to

3条回答
  •  悲哀的现实
    2021-01-04 14:32

    This actually drove me a bit crazy. In the end I fixed it like this:

    • Created a Shared.config file in the dll project 'common', having the contents look like any ordinary web.config/app.config.
    • Set the file to be Content and Copy Always, so it would surely be copied out to all projects that reference project common. (Though the config file will indeed end up in the bin folder.
    • Created the class SharedConfiguration inside the common project. The really tricky part was having to use OpenMappedExeConfiguration() , and getting the path to the executable directory (including bin, and without file:// in front of it).
    • Now when I want to access a setting from the shared settings, I do SharedConfiguration.instance.AppSettings.Settings["CubilisEntryPointUrl"].Value.

    (I cannot use SharedConfiguration.instance.AppSettings["CubilisEntryPointUrl"] directly because of this issue)

    .

    public static class SharedConfiguration
    {
        public static readonly Configuration instance = GetConfiguration("Shared.config");
        private static Configuration GetConfiguration(string configFileName)
        {
            ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap();
            Uri uri = new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));
            exeConfigurationFileMap.ExeConfigFilename = Path.Combine(uri.LocalPath, configFileName);
            return ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, ConfigurationUserLevel.None);
        }
    }
    

提交回复
热议问题