[英]Visual Studio incorrectly referencing assembly from nuget package /build directory
我有一個C#項目(ProjectA),它在一個單獨的進程中調用另一個C#項目(ProjectB)。 構建輸出目錄結構是:
/ProjectA.exe
/ProjectB/ProjectB.exe
ProjectA和ProjectB引用相同程序集的差異版本,在本例中為Newtonsoft.Json.dll。
通過向ProjectA添加ProjectB的nuget包來實現構建輸出目錄結構。 ProjectA和ProjectB在單獨的解決方案中並單獨構建。 ProjectB的nuget包是使用以下.nuspec和.targets創建的。
<?xml version="1.0"?>
<package>
<metadata>
<id>ProjectB</id>
<version>$version$</version>
<title>ProjectB</title>
<authors>me</authors>
<owners>me</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>ProjectB</description>
<copyright>Copyright 2016</copyright>
<tags>ProjectB</tags>
</metadata>
<files>
<file src="x64\Release\*" target="build" />
<file src="ProjectB.targets" target="build/ProjectB.targets" />
</files>
</package>
。
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<NativeLibs Include="$(MSBuildThisFileDirectory)*" />
<Content Include="@(NativeLibs)">
<Link>ProjectB\%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
我的問題是ProjectA引用了ProjectB nuget包/ build目錄中較新的Newtonsoft.Json.dll,而不是ProjectA解決方案中較舊的Newtonsoft.Json.dll。 它們是不同的版本,因此這會在運行時導致問題。 我意識到我可以在ProjectA解決方案中更新Newtonsoft.Json.dll的版本,但我希望能夠在不可能的情況下解決更一般的情況。 如何防止Visual Studio找到錯誤的Newtonsoft.Json.dll?
編輯:這個答案實際上沒有用。 VisualStudio的行為與它決定使用哪個程序集非常不一致。 有時從輸出目錄中刪除程序集將導致最終使用的程序集版本發生更改。 顯然,任何鏈接到項目的程序集文件都可以在編譯期間由MSBuild選擇。 請改為查看我的其他答案 。
我發現如果我將所有ProjectB程序集放在nuget package / build目錄的單獨子目錄中,那么MSBuild將不會將它們用作引用。 ProjectB的工作nuget包是使用以下.nuspec和.targets創建的。
<?xml version="1.0"?>
<package>
<metadata>
<id>ProjectB</id>
<version>$version$</version>
<title>ProjectB</title>
<authors>me</authors>
<owners>me</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>ProjectB</description>
<copyright>Copyright 2016</copyright>
<tags>ProjectB</tags>
</metadata>
<files>
<file src="x64\Release\*" target="build/ProjectB" />
<file src="ProjectB.targets" target="build/ProjectB.targets" />
</files>
</package>
。
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<NativeLibs Include="$(MSBuildThisFileDirectory)ProjectB\*" />
<None Include="@(NativeLibs)">
<Link>ProjectB\%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
顯然,MSBuild在與ProjectB.targets文件相同的目錄中查找匹配程序集,但不查找子目錄。 MSBuild的這種“神奇”行為應該由Microsoft修復。 請參閱此問題的相關評論
最后,將鏈接的項目組添加到所有ProjectB程序集總是會導致將錯誤的程序集版本拉入ProjectA時出現問題。 我不得不求助於robocopy postbuild命令來做我想做的事。 這有一個缺點,即當ProjectA被另一個項目引用時,ProjectB程序集不會被復制,但這對我來說並不重要。 如果Microsoft曾經使用魔術程序集路徑修復此行為,那么原始問題中的nuget包應該可以正常工作。 以下是帶有robocopy解決方法的.nuspec和.targets文件。
<?xml version="1.0"?>
<package>
<metadata>
<id>ProjectB</id>
<version>$version$</version>
<title>ProjectB</title>
<authors>me</authors>
<owners>me</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>ProjectB</description>
<copyright>Copyright 2016</copyright>
<tags>ProjectB</tags>
</metadata>
<files>
<file src="x64\Release\*" target="build/ProjectB" />
<file src="ProjectB.targets" target="build/ProjectB.targets" />
</files>
</package>
。
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="ProjectBCopyTarget" AfterTargets="Build">
<Exec Command="robocopy /PURGE $(MSBuildThisFileDirectory)ProjectB $(TargetDir)ProjectB || if %ERRORLEVEL% LSS 8 exit 0"/>
</Target>
</Project>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.