Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solution wide app.config / web.config?

I have a solution with one asp.net-mvc-2 project, one web service, one class library and three normal desktop apps. Currently I need to copy all app.config settings from one project to the other. That is a nightmare if I want to change something.

I know there is a solution to store all these information in the global .config files, but I am looking for a solution to store the values dedicated to my solution not to my machine. Is that possible?

like image 359
Chris Avatar asked Jul 01 '10 13:07

Chris


People also ask

Is app config the same as Web config?

Web. Config is used for asp.net web projects / web services. App. Config is used for Windows Forms, Windows Services, Console Apps and WPF applications.

Where can I find Web config?

The Web. Config file is used to configure Oracle Web Application functionality. This file is typically installed in the c:\Inetput\wwwroot\WebApp directory.

What is Web config in C#?

A configuration file (web. config) is used to manage various settings that define a website. The settings are stored in XML files that are separate from your application code. In this way you can configure settings independently from your code. Generally a website contains a single Web.

What are the three policies in configuration files?

This article describes the syntax of configuration files and provides information about the three types of configuration files: machine, application, and security.


4 Answers

You can add a single .config file to one of your projects, and then choose Add -> Existing Item... -> Add As Link to add a reference to that file to your other projects. They will all build with the file as if it was their own copy, but there will really only be one copy, and you will only have to make changes in a single place.

like image 171
Dave Cousineau Avatar answered Oct 20 '22 20:10

Dave Cousineau


I have found a really simple solution here:

1. Create a file CommonSettings.config In your Common, Class library project.

2. Put your common setting in this file:

<appSettings>
    <add key="someCommonSetting" value="some value"></add>
    <!-- more setting here -->
</appSettings>

Note: <appSetting> has to be the root element in your CommonSettings.config (don't put it under <configuration>)

3. Make sure CommonSettings.config file is copied to output directory:

enter image description here

4. In all other project's App.Config/Web.config files, add the above common settings

That's it... You common settings will be included in every other config file.

<appSettings file="CommonSettings.config">
    <add key="Value1" value="123" />
</appSettings>  

Note:

For this approach to work, the shared config file should be copied to the project’s output directory so it is adjacent to the regular App/Web.config file. Add the existing shared .config file to the project as a Linked file and set it to ‘Copy if newer’. You should see something similar to this in your .csproj file:

<None Include="..\CommonConnectionStrings.config">
  <Link>CommonConnectionStrings.config</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

You can do the same for ConnectionString, copy all your connection strings in CommonConnectionStrings.config, and in other App.Config/Web.Config, reference it like this:

<connectionStrings configSource="CommonConnectionStrings.config" />

Note: If you are using this solution for connectionStrings, you cannot have a project specific connection string, all of the connection strings will be copied from the common config.

like image 30
Hooman Bahreini Avatar answered Oct 20 '22 19:10

Hooman Bahreini


Assuming you could have a path where both your website and webservice could access it, then you could potentially use the appsettings " file " attribute to have the settings stored in one common place. Refer http://www.codeproject.com/KB/dotnet/appsettings_fileattribute.aspx Hope this helps!

like image 39
Jagmag Avatar answered Oct 20 '22 20:10

Jagmag


I would comment on the answer @Sahuagin gave, but I don't have enough reputation. This does work with App.config files, but you have to add a text file, then call it App.config. After that just declare the XML (as seen below)

<configuration>
   <!-- add you values or whatever here-->
</configuration>

Then do as @Sahuagin said and add it as a link to your solution. It works a treat.

like image 38
TechnicalTophat Avatar answered Oct 20 '22 19:10

TechnicalTophat