警告NETSDK1080:当针对.NET Core 3.0或更高版本时,不需要将PackageReference设置为Microsoft.AspNetCore.App

15

当我通过dotnet test命令行运行.NET Core测试时,我如何解决令人讨厌的警告?

dotnet --version返回值为3.1.101

$ dotnet test
watch : Started
C:\Program Files\dotnet\sdk\3.1.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets(151,5):
    warning NETSDK1080: A PackageReference to Microsoft.AspNetCore.App is not necessary when targeting
    .NET Core 3.0 or higher. If Microsoft.NET.Sdk.Web is used, the shared framework will be referenced automatically.
    Otherwise, the PackageReference should be replaced with a FrameworkReference.
    [C:\github\demo\Demo\SmartHome.API\SmartHome.API.csproj]
C:\Program Files\dotnet\sdk\3.1.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets(151,5):
    warning NETSDK1080: A PackageReference to Microsoft.AspNetCore.App is not necessary when targeting
    .NET Core 3.0 or higher. If Microsoft.NET.Sdk.Web is used, the shared framework will be referenced automatically.
    Otherwise, the PackageReference should be replaced with a FrameworkReference.
    [C:\github\demo\Demo\SmartHome.API\SmartHome.API.csproj]
Test run for C:\github\demo\Demo\SmartHome.API.Test\bin\Debug\netcoreapp3.1\SmartHome.API.Test.dll(.NETCoreApp,Version=v3.1)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

A total of 1 test files matched the specified pattern.
这是我的 SmartHome.API.Test.csproj 文件的样子。
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="FluentAssertions" Version="5.10.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
    <PackageReference Include="MongoDB.Driver" Version="2.10.1" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
    <PackageReference Include="coverlet.collector" Version="1.0.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\SmartHome.API\SmartHome.API.csproj" />
    <ProjectReference Include="..\SmartHome.Models\SmartHome.API.Models.csproj" />
  </ItemGroup>

</Project>

这是导致问题的源头,名为SmartHome.API.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="FluentValidation" Version="8.6.1" />
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.1" />
    <PackageReference Include="MongoDB.Driver" Version="2.10.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\SmartHome.Models\SmartHome.API.Models.csproj" />
  </ItemGroup>

</Project>
2个回答

35

SmartHome.API.csproj中的包依赖从Microsoft.AspNetCore.App (2.2.8) 改为 FrameworkReference,解决了我的问题,但引入了一个新问题。

初始修复

+  <ItemGroup>                                                                                                                         
+    <FrameworkReference Include="Microsoft.AspNetCore.App" />                                                                         
+  </ItemGroup>                                                                                                                        
+                                                                                                                                      
   <ItemGroup>                                                                                                                         
     <PackageReference Include="FluentValidation" Version="8.6.1" />                                                                   
-    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8" />                                                           
     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.1" />                                      
     <PackageReference Include="MongoDB.Driver" Version="2.10.1" />                                                                    
   </ItemGroup>                                                                                                                        

新警告

我开始看到一个新的警告:

C:\Program Files\dotnet\sdk\3.1.101\Sdks\Microsoft.NET.Sdk\targets\
  Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(39,5):
warning NETSDK1086: A FrameworkReference for 'Microsoft.AspNetCore.App' was 
  included in the project. This is implicitly referenced by the .NET SDK and you
  do not typically need to reference it from your project. For more information,
  see https://aka.ms/sdkimplicitrefs

最终修复

……所以,我最终将"Microsoft.AspNetCore.App"引用完全删除。现在编译没有警告了!

也就是说,文件看起来像这样:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="FluentValidation" Version="8.6.1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.1" />
    <PackageReference Include="MongoDB.Driver" Version="2.10.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\SmartHome.Models\SmartHome.API.Models.csproj" />
  </ItemGroup>

</Project>                                                                                                                 

1
非常感谢提供的修复方法。 我删除了引用<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8" />,现在我的构建也没有警告了! :) 祝好, 哈拉尔德

1
请不要在回答中发表评论,而是将解决您问题的答案标记为正确答案。 - Mouse On Mars
这不是对问题的回答。一旦您获得足够的声望,您就可以评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - Reza Heidari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接