Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio/MSBuild copy referenced class library's app.config as *.dll.config to bin folder of current project

I have a class library that is referenced by many other web application projects. It has many settings in its app.config that I wish to use in all of the referencing web projects. When the class library is built, it copies the app.config to its own bin folder as <assembly.name>.dll.config.

How do I ensure that <assembly.name>.dll.config is copied to the bin folder of each of my referencing web application projects?

  • Visual Studio / MSBuild does not seem to do this by default.
  • Changing Copy to Output Directory or Build Action (in any combination) does not work.
  • SlowCheetah VS extension appears to do what I want as an unintended side-effect of its config transform process, but I want to do this without any 3rd-party extensions.

As an aside: It's impractical to manually put all of this config in each of the web application projects. I can read the file by looking for <assembly.name>.dll.config in the bin folder, and extract the settings from there. I can already do this, so that's not an issue - I just need to ensure the file is going to be there for reading

like image 386
theyetiman Avatar asked Jan 15 '16 12:01

theyetiman


People also ask

Can class library have app config?

Class libraries can access configuration settings in the same way as executable apps, however, the configuration settings must exist in the client app's App. config file.

What is the use of app config in c#?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.

Does app config get compiled?

Now you might be wondering what happens behind the scenes. Well, when you compile your application, the compiler actually copies the app. config file to the output folder, but gives it another name: When you start your application (ConsoleApp1.exe in our example), the matching config file will be loaded too.


2 Answers

This can be achieved without 3rd-party tools by adding the following to the class library's project file, assembly.name.csproj:

// Find this <Import> element for Microsoft.CSharp.targets
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
// Add this <ItemGroup> element immediately after
<ItemGroup>
    <Content Include="app.config">
        <Link>$(TargetName).dll.config</Link>
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>

This causes the app.config to be copied to whichever project is referencing it as <assembly.name>.dll.config. It's good because you only need to configure the one .csproj file and the effect cascades out to all referencing projects.

like image 132
theyetiman Avatar answered Oct 14 '22 22:10

theyetiman


Visual Studio 2015 and earlier

For Visual Studio 2015, I am using the solution of @theyetiman (see this answer):

<ItemGroup>
  <None Include="app.config" />
</ItemGroup>
<ItemGroup>
  <None Include="app.config">
    <Link>$(TargetFileName).config</Link>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>

(The import of Microsoft.CSharp.targets was already present.)

Visual Studio 2017

During my experiments, Visual Studio 2017 seems to handle configuration files as expected out of the box. No manual modification of the project file is needed.

like image 35
CodeFox Avatar answered Oct 14 '22 23:10

CodeFox