Single config file for solution

后端 未结 3 2080
旧时难觅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:16

    If your hosting in IIS it is possible to have a single web.config file at the root site level but Giorgio is right in that app.config files are app specific. it is possible to use custom build steps to automate the copying of config files across multiple projects so personally I would go with that.

    0 讨论(0)
  • 2021-01-04 14:20

    The config files are app specific. This mean that you can add a config file to a class library but the file will then by used by the app (windows service, webservice and so on) referencing the library.

    Same thing for external configSource, this are app specific as well and need to be included withing the project using it. So if your solution is composed by 2 projects you then need 2 config files. One for each project.

    While for a windows based application(services, winforms) the expected folder for config files is the bin directory, for web based projects this will be the directory is the root folder of the virtual directory.

    This said, using a shared config file looks the easier solution (and you don't have to copy the app.config from the class library for each project). Here are the steps :

    • Create a solution folder.
    • Add the config file to it.
    • Add the file as a reference for each project needing it. Right click the project and Add existing item - > Choose the file and Add as link
    • Ensure the file is always copied by setting the copy option (properties of the file) with Copy Always.

    At this point you should have the config file deployed into your project directory everytime you compile the solution.

    EDIT:

    • I'd avoid looking into the bin for config files within a web app, the convention is that file should be in the root, so I would avoid the first option.
    • Linked files end up in the bin after building the project. Try the same steps for importing the file but this time simply add it (not as link) and it will be deployed as content in the root of your site, so it can be always available.
    0 讨论(0)
  • 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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题