Visual Studio/MSBuild将引用的类库的app.config作为* .dll.config复制到当前项目的bin文件夹中 [英] Visual Studio/MSBuild copy referenced class library's app.config as *.dll.config to bin folder of current project

查看:210
本文介绍了Visual Studio/MSBuild将引用的类库的app.config作为* .dll.config复制到当前项目的bin文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类库,该类库被许多其他Web应用程序项目引用.我希望在所有引用Web项目中使用其app.config中的许多设置.构建类库后,它将app.config复制为<assembly.name>.dll.config到其自己的bin文件夹中.

如何确保将<assembly.name>.dll.config复制到引用的Web应用程序项目的每个的bin文件夹中?

  • 默认情况下,Visual Studio/MSBuild似乎不执行此操作.
  • 更改Copy to Output DirectoryBuild Action(以任何组合)均无效.
  • SlowCheetah VS扩展似乎完成了我想要的配置转换过程的意外操作,但是我想在没有任何第三方扩展的情况下进行

顺便说一句:手动将所有此配置放在每个Web应用程序项目中是不切实际的.我可以通过在bin文件夹中查找<assembly.name>.dll.config来读取文件,然后从中提取设置.我已经可以做到这一点,所以这不是问题-我只需要确保文件可以在其中读取即可

解决方案

在没有第三方工具的情况下,可以通过将以下内容添加到类库的项目文件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>

这将导致将app.config复制到引用它为<assembly.name>.dll.config的任何项目.很好,因为您只需要配置一个.csproj文件,并且效果会级联到所有引用项目中.

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

解决方案

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.

这篇关于Visual Studio/MSBuild将引用的类库的app.config作为* .dll.config复制到当前项目的bin文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆