集成和单元测试在ASP.NET Core 2.1上不再起作用,无法在运行时找到程序集 [英] Integration and unit tests no longer work on ASP.NET Core 2.1 failing to find assemblies at runtime
问题描述
在创建测试项目或升级应用程序并测试到ASP.NET Core 2.1 / .NET Core 2.1时,运行测试失败,并带有程序集加载异常,例如
When creating test projects or upgrading an application and tests to ASP.NET Core 2.1 / .NET Core 2.1, running tests fails with assembly load exceptions like
System.IO.FileNotFoundException:无法加载文件或程序集'Microsoft.AspNetCore,Version = 2.1.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60'。系统找不到指定的文件。
System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
在添加对其他某些库的引用时,还会出现类似
When adding references to some other libraries there are also build warnings like
警告MSB3277:发现不同版本的 Microsoft.Extensions.Options之间的冲突无法解决。
警告MSB3277:发现冲突
警告MSB3277:发现不同版本的 Microsoft.AspNetCore.Hosting.Abstractions之间的冲突无法解决。
警告MSB3277:发现无法解决的不同版本的 Microsoft.Extensions.DependencyInjection.Abstractions之间的冲突。
警告MSB3277:发现不同版本的 Microsoft。无法解决的AspNetCore.Http.Abstractions。
警告MSB3277:发现不同版本的 Micr之间存在冲突osoft.AspNetCore.Http.Features无法解决。
warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.Options" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.Configuration.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Hosting.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.DependencyInjection.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Http.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Http.Features" that could not be resolved.
如何使测试项目能够测试ASP.NET Core 2.1应用程序?
How can I make test projects work for testing ASP.NET Core 2.1 applications?
推荐答案
更新: 2.2工具简化了此操作强>。确保您的 dotnet --version
SDK版本至少为 2.2.100
,甚至在构建2.1时也是应用程序
Update: This has been made easier with 2.2 Tooling. Make sure that your dotnet --version
SDK version is at least 2.2.100
, even when buidling 2.1 applications
只需在保持 Microsoft.NET.Sdk
的同时向项目添加无版本的包引用即可:
Just add a versionless package reference to your project while keeping the Microsoft.NET.Sdk
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<!-- other references to xunit, test SDK etc. -->
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AspNetCoreAppToTest\AspNetCoreAppToTest.csproj" />
</ItemGroup>
</Project>
原始:
ASP.NET Core 2.1使用新的共享框架在ASP.NET Core应用程序上运行。需要修改/更新测试项目以使用以下方法之一来使用此共享框架:
ASP.NET Core 2.1 uses a new "shared framework" to run ASP.NET Core applications on. Test projects need to be modified/updated to also use this shared framework using one of the following approaches:
-
更改测试项目的第一行中的
< Project>
标记以使用Web SDK(Microsoft.NET.Sdk.Web
Microsoft.NET.Sdk
),并添加对Microsoft.AspNetCore.App
(或.All
(如果您在网络项目中使用的话),而未指定版本
Change the test project's
<Project>
tag in the first line to use the web SDK (Microsoft.NET.Sdk.Web
instead ofMicrosoft.NET.Sdk
) and add a package reference toMicrosoft.AspNetCore.App
(or.All
if you are using that inside the web project) without specifying a version
项目文件( .csproj)现在应该看起来像这样:
The project file (.csproj) of the test project should now look like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<!-- other references to xunit, test SDK etc. -->
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AspNetCoreAppToTest\AspNetCoreAppToTest.csproj" />
</ItemGroup>
</Project>
替代方法:保留 Sdk
并在共享框架包中添加 PackageReference
,但指定版本。
Alternative: Leave the Sdk
as-is and add a PackageReference
to the shared framework package but specify a version.
只需将NuGet引用添加到 Microsoft.AspNetCore.App
。但是,这可能会引起问题,因为在发布ASP.NET Core的新修补程序版本时,SDK可能会选择更新引用,并且工具也会进行更新以反映这一点。您需要为每个修补程序版本更新NuGet参考。
This can be done by simply adding a NuGet reference to Microsoft.AspNetCore.App
. However, this may cause issues since the SDK may choose to update the reference when a new patch release of the ASP.NET Core is released and the tooling is updated to reflect this. You will need update the NuGet reference for every patch release.
这篇关于集成和单元测试在ASP.NET Core 2.1上不再起作用,无法在运行时找到程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!