好的,经过几个小时的努力思考/挖掘,我想我找到了正确的解决方案。
将在此处发布以供参考。
对 dotnet cli 问题的交叉引用:https://github.com/dotnet/cli/issues/6032
我有一个基于 .Net 4.5、x86 的现有遗留解决方案,我们称之为 oldsolution.sln
我有一个新的解决方案,其中包含来自 oldsolution.sln 的一些项目,以及一些新的 dotnet 核心项目,我们称之为 mySolution.sln。
新的解决方案还需要为 .Net 4.5 构建到 x86
建筑
这里要掌握的关键是 Visual Studio 2017 使用安装文件夹中的 msbuild.exe 进行构建。 dotnet msbuild 使用来自 dotnet sdk 文件夹的程序集。而且这些并不完全相同。
所以要像 Visual Studio 那样构建,我必须找到正确的 msbuild 可执行文件才能使用。
Visual Studio 团队为此开发了一个工具 (https://www.nuget.org/packages/vswhere),我在构建脚本中使用它。
有了它,一切正常。 (构建明智)
下面是我构建和测试解决方案的 PSake 脚本。
Task BuildApi {
exec { msbuild ./oldSolution.sln /t:Rebuild /p:Configuration="Release" /p:Platform=x86 /m /verbosity:minimal /nr:false }
exec { dotnet restore .\mySolution.sln }
#Find location of VS2017
$VsPath = .\packages\vswhere.1.0.50\tools\vswhere.exe -latest -property installationPath
$msBuild17 = "$vsPath\MSBuild\15.0\Bin\MSBuild.exe"
exec { &$msbuild17 ./mySolution.sln /t:Build /p:Configuration=Release /p:Platform=x86 /m /verbosity:minimal /nr:false }
}
Task TestApi -depends BuildApi{
if(!(Test-Path TestResults))
{
mkdir TestResults
}
if(!(Test-Path TestResults/coverage))
{
mkdir TestResults/coverage
}
$coverage = './packages/OpenCover.4.6.519/tools/OpenCover.Console.exe'
$target = "`"C:\Program Files (x86)\dotnet\dotnet.exe`""
$filter = "`"+[WebApi]*`""
#UnitTests
$targetargs = "`"test --no-build .\WebApi\test\WebApi.UnitTests\WebApi.UnitTests.csproj -c Release --logger `"trx;LogFileName=UnitTests.trx`"`""
$output = 'TestResults/coverage/WebApi.UnitTests.Coverage.xml'
&$coverage -register:user -oldstyle -target:$target -targetargs:$targetargs -output:$output -filter:$filter
# IntegrationTests
$targetargs = "`"test --no-build .\Web\test\WebApi.IntegrationTests\WebApi.IntegrationTests.csproj c Release --logger `"trx;LogFileName=IntegrationTests.trx`"`""
$output = 'TestResults/coverage/WebApi.IntegrationTests.Coverage.xml'
&$coverage -register:user -oldstyle -target:$target -targetargs:$targetargs -output:$output -filter:$filter
#Generate HTML report
$reportGenerator = "./packages/ReportGenerator.2.4.5.0/tools/ReportGenerator.exe"
$reportFiles = "TestResults/coverage/WebApi.UnitTests.Coverage.xml;TestResults/coverage/WebApi.IntegrationTests.Coverage.xml"
$targetDir = "./TestResults/coverage/WebApi"
&$reportGenerator -reports:$reportFiles -targetdir:$targetDir
}
到目前为止一切顺利。
测试
然后我在让 OpenCover 获取覆盖结果时遇到了麻烦。
发现 XUnit 做了一个 shadowCopy - 解决这个问题的方法是在每个测试项目中放置一个 xunit.runner.json (https://xunit.github.io/docs/configuring-with-json.html)
{
"shadowCopy": false
}
告诉 XUnit 不要做影子复制,这样 OpenCover 就可以找到与被测可执行文件匹配的 PDB 文件。
终于……
从 Visual Studio 中运行测试,所有使用 Microsoft.AspNetCore.TestHost.TestServer 引用某些 dotnet 核心程序集的测试都失败了,因为“某些东西”引用了 v 1.0.0 版本的程序集 - 而我的项目引用了 v 1.1。 X。
我已通过在 app.config 文件中为所有失败的程序集进行程序集重定向来解决此问题。无法弄清楚谁/什么使用了旧的 1.0.0 程序集 - 但 VS 2017 构建链中的某些东西似乎可以做到这一点 - 因为它适用于 dotnet 测试。
但是,这是我的 app.config 的副本,其中重定向放置在我的集成测试项目中。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">"
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.DependencyInjection.Abstractions" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.AspNetCore.Mvc.Core" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.1.2.0" newVersion="1.1.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.Options" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.AspNetCore.Http.Abstractions" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.AspNetCore.StaticFiles" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.FileProviders.Abstractions" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.Primitives" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.AspNetCore.Routing" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.AspNetCore.Routing.Abstractions" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.AspNetCore.Mvc.Formatters.Json" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.1.2.0" newVersion="1.1.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.AspNetCore.Mvc.ApiExplorer" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.1.2.0" newVersion="1.1.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
呼……那是一场艰苦的战斗。
但物有所值。
与旧版 x86、net 4,5 程序集、在全新的 .Net Core Web Api 解决方案中运行的组合解决方案有效 - 我很高兴.. :-)