MSBuild 不获取引用项目的引用
我刚才在使用 MSBuild 时遇到了一个奇怪的情况。 有一个解决方案包含三个项目:LibX、LibY 和 Exe。 Exe 引用 LibX。 LibX 又引用 LibY,有一些内容文件,还引用第三方库(GAC 和本地 lib 文件夹中安装的几个预构建程序集)。 第三方库被标记为“复制本地”(“私有”)并出现在 LibX 项目的输出中,就像 LibY 的输出和 LibX 的内容文件一样。 现在,Exe 项目的输出具有 LibX 项目输出、LibX 项目的内容文件、LibY 项目输出(来自 LibX),但没有第三方库的程序集。
现在我通过直接在 Exe 项目中引用第三方库来解决这个问题,但我不认为这是一个“正确”的解决方案。
以前有人遇到过这个问题吗?
I bumped into a strange situation with MSBuild just now. There's a solution which has three projects: LibX, LibY and Exe. Exe references LibX. LibX in its turn references LibY, has some content files, and also references to a third-party library (several pre-built assemblies installed in both GAC and local lib folder). The third-party library is marked as "Copy Local" ("private") and appears in the output of the LibX project, as the LibY's output and LibX's content files do. Now, Exe project's output has LibX project output, content files of the LibX project, LibY project output (coming from LibX), but NO third-party library's assemblies.
Now I worked this around by referencing the third-party library directly in Exe project, but I don't feel this is a "right" solution.
Anyone had this problem before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
与使用 Visual Studio 构建相比,使用 MSBuild(即命令行、TFS Build 和其他工具)构建时的行为存在差异。 辅助引用不包含在发送到 MSBuild 编译任务的引用变量中。
MSBuild 提供了多个扩展点来更改引用的解析方式。 我已成功使用 AfterResolveReference 解决了我的一些项目的此问题 - 我已经在我的博客上发布了有关背景的更多信息。
解决方法是将以下代码添加到 vbproj 或 csproj 文件中
Microsoft 已声明此问题无法修复 连接
There is a difference in behavior when building with MSBuild (i.e. command line, TFS Build and other tools) compared to building with Visual Studio. The secondary references are not included in the references variable sent into MSBuild compile tasks.
There are several extension points provided by MSBuild to change how references are to be resolved. I have successfully used AfterResolveReference to fix this issue for some of my projects - I have posted more info about the background on my blog.
The workaround is to add the following code into you vbproj or csproj files
Microsoft has stated that this is a won't fix on Connect
您实际上可以进入 Microsoft.CSharp.targets 或 Microsoft.VisualBasic.targets 文件(位于框架目录中,通常为 C:\Windows\Microsoft.NET\Framework\v3.5)并将 csc 或 vbc 任务参数修改为包括额外的参考依赖项。 在文件(VB 目标,第 166 行;C# 目标,第 164 行)中,将:\ 更改
为
这可能会导致其他问题,具体取决于事情的复杂程度,并且它可能会与 Visual Studio inproc 编译器一起使用,但这是唯一的方法我在 MSBuild 中找到了它。
You can actually go into the Microsoft.CSharp.targets or Microsoft.VisualBasic.targets file (located in the framework directory, usually C:\Windows\Microsoft.NET\Framework\v3.5) and modify the csc or vbc task parameters to include additional reference dependencies. In the file (VB targets, line 166; C# targets, line 164) change:\
to
This might cause other issues depending on how complicated things are and it may play tricks with the Visual Studio inproc compiler, but it's the only way to do it in MSBuild that I've found.
乔桑的回答几乎对我有用; 当我尝试这样做时,我在 Visual Studio 中不断收到错误:
尝试为 IDE 的进程内编译器设置“引用”参数时出现问题。 错误 HRESULT E_FAIL 已从对 COM 组件的调用返回
我的问题的解决方案是在 ItemGroup 上设置一个条件,如下所示:
这导致 Visual Studio 完全忽略引用更改,并且构建工作正常本地和构建服务器上。
josant's answer almost worked for me; I kept getting an error in Visual Studio when I tried that:
A problem occurred while trying to set the "References" parameter for the IDE's in-process compiler. Error HRESULT E_FAIL has been returned from a call to a COM component
The solution to my problem was to put a condition on the ItemGroup, like this:
That caused Visual Studio to ignore the reference change completely, and the build works fine locally and on the build server.
是的,我也遇到过这个问题。 尽管我很想说,但我相信您必须将所有传递依赖项作为引用包含在构建文件中。
Yes, I've had that problem, too. Though I'd love to say otherwise, I believe you must include all transitive dependencies as references in your build file.
我结合了 Alex Yakunin 的 解决方案还可以复制本机 dll< /a>.
I've combined Alex Yakunin's solution with one that will also copy native dll's.
如果您得到的是有向图而不是树,并且出现“尝试部署 dll 的不同副本”错误,则 AfterResolveReferences 方法会失败。 (参见如何配置 msbuild/ MSVC部署依赖程序集的依赖文件)
The AfterResolveReferences method fails if you've got a directed graph not a tree with a "trying to deploy different copies of the dll" error. (cf. How to configure msbuild/MSVC to deploy dependent files of dependent assemblies)