10

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 talk to each other. After publishing each of these web services might end up on a different machine with a different database. To tell each web service where all other web services are, I want to maintain a single config file during development.

I would like to expect that after publishing the config to be present in each published project. And I would like to expect the config file to be editable after publishing, so I can swiftly migrate a certain web service and then just edit all config files of the other web services.

I don't want to do this in the database, for the config file its self should also hold connection settings to the database(s).

I came across the following ideas/thoughts/questions:

  • I have a dll project called 'common' that is referenced by other projects. Let's give that one a shared.config and build a class in that project that can be used to read out the shared.config by doing System.Configuration.ConfigurationManager.OpenExeConfiguration("shared.config"). Just need to make sure the shared.config will be published along with the DLL.

I would favor this solution, as it would also let me keep a web.config inside each project having just the project specific settings. And have the shared.config having the shared settings. But I read on SO that this should not be considered lightly and could have some unwanted side-effects, like file-access-issues; though I wonder if this would apply to my case. Also I would like to ask your help here on how to actually realize this as I don't think Visual Studio supports app.config for DLL projects out of the box.

  • I also thought about creating a shared.config file in the Solution Items. Then linking that file inside each project. And in the Web.config of each projects, add: <appSettings configSource="shared.config" /> pointing to the linked file in that project.

Though I cannot find any reason why not to do this, first implementation failed. It seems (at least during development), c# cannot find the linked shared.config file. I'm guessing linking files is not done instantly nor maintained after creating the linked file, but the file is only copied to the projects WHEN I do a publish. Thus leaving the file missing during development. Is this correct?

3
  • 1
    what's your publishing strategy? Is it a continuous integration environment or manual publishing? Mar 19, 2013 at 9:49
  • 2
    I'm sorry to say that I am new to .NET, c# and visual studio. I don't know what a continuous integration environment means and tend to guess that I do manual publishing.
    – nl-x
    Mar 19, 2013 at 9:51
  • 2
    Continuous integration is where you set up some kind of service to checkout, build, test and deploy your code every time you make a change - with various caveats and conditions appropriate to your project. It's neither limited to or ubiquitous within .net by any means - many tools have their roots in the Java ecosystem for example; and I'm sure there are many .net developers who don't use any such thing. Take a look at jenkins-ci.org for an example of a CI system.
    – Tom W
    Mar 19, 2013 at 10:29

3 Answers 3

6

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.
2
  • First off, I was mistakenly talking about app.config. I already knew that a project will look at its own config file and not to the one of the DLL project. So I have edited my question to clarify that I meant to say to use System.Configuration.ConfigurationManager.OpenExeConfiguration(string exePath) and load a 'shared.config' from within the DLL file. But I wonder if this is wise to do, as I have read about this having side effects like file-access issues when for example reusing the DLL file from different application. But I wonder if this would apply to me. I don't think I reuse the DLL.
    – nl-x
    Mar 19, 2013 at 10:40
  • Secondly, I already was using "add as link". But the linked file seemed to be not readable during development. I think linked files are only being copied during a publish. It that correct? I already tried Copy Always, but it didn't help me out...
    – nl-x
    Mar 19, 2013 at 10:42
0

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.

3
  • I was a bit confused when I spoke of app.config. I have edited my question and stated that I meant to just read an external config file from within the DLL project, using System.Configuration.ConfigurationManager.OpenExeConfiguration(string exePath)
    – nl-x
    Mar 19, 2013 at 10:30
  • So lets get this straight, your trying to create a single project that contains a config file that you want to share amongst many projects.
    – Mintey
    Mar 19, 2013 at 10:35
  • That could be one way to go, HeyItsJames. That is what would be covered in the first bullet point of my question. (The second bullet points covers a way in which I just have a file that is linked to inside the other projects. So the second bullet points does not involve a single project's config file being shared by others.)
    – nl-x
    Mar 19, 2013 at 10:45
0

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);
    }
}
1
  • I still don't like my own answer myself as I still am working with/in the bin directory. To be able to just work in the root directory I will have to figure out how to make linked items exist even before I publish the project. In order to do this I started a new question here: stackoverflow.com/questions/15505817/…
    – nl-x
    Mar 19, 2013 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.