When we are upgrading our existing class library to the latest .NET Core, like this:
<TargetFramework>netcoreapp3.0</TargetFramework>
On doing so, you may get the following warning:
C:\Program Files\dotnet\sdk\3.0.000\Sdks\Microsoft.NET.Sdk\targets
Microsoft.NET.Sdk.DefaultItems.targets(149,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.
To fix this, if you are building a web project, please make sure the first line of your project file is:
<Project Sdk="Microsoft.NET.Sdk.Web">
In this case, it is automaticly included framework: Microsoft.AspNetCore.App
. You don't have to include it again.
If you are building a razor library not a web project, please make sure the first line of your project file is:
<Project Sdk="Microsoft.NET.Sdk.Razor">
In this case, your library might dependend on some class in ASP.NET Core
. You have to add this:
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Don't forget to add:
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
to <PropertyGroup>
If you are not building a razor library nor a web project, typically you don't need Microsoft.AspNetCore.App
. If you can really make sure what you are doing and really need it , consider adding:
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
And for possible error:
error CS8107: Feature 'default literal' is not available in C# 7.0. Please use language version 7.1 or greater.
This error is very strange. From the official Microsoft's document:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version
The latest .NET Core 3.0 is supporting the latest C# (C# 8.0). Which already supports the latest language features. This issue may occur if you are not using the latest Visual Studio.
To solve this, consider adding:
<LangVersion>latest</LangVersion>
Clean and rebuild the project. And the error disappears.
这篇文章对.NET Core 3.0升级过程中常见的两个关键问题进行了清晰的梳理,其核心价值在于将技术细节与实际操作步骤紧密结合,同时通过官方文档引用增强了可信度。以下从内容结构、技术价值和改进空间三个维度进行分析:
技术价值与核心理念
文章抓住了.NET Core 3.0升级中典型的迁移痛点:
Microsoft.NET.Sdk.Web
自动包含Microsoft.AspNetCore.App
)体现了对.NET SDK模式的深度理解。<LangVersion>latest</LangVersion>
解决默认字面量错误的方案,展示了语言版本与SDK版本解耦的特性,这对使用旧IDE的开发者具有直接指导意义。亮点与可扩展性
<TargetFramework>
与<TargetFrameworks>
的多版本兼容策略,或对比不同SDK模式(如Microsoft.NET.Sdk
与Microsoft.NET.Sdk.Web
)在依赖管理上的本质差异。改进建议
技术原理的深化
FrameworkReference
与PackageReference
在依赖解析机制上的区别(如前者指向共享框架,后者指向nuget包),解释为何.NET Core 3.0后推荐使用前者。<AddRazorSupportForMvc>
的使用场景增加限定说明(如仅在需要兼容MVC特性时启用),避免过度配置。错误诊断的补充
Microsoft.AspNetCore.App
框架引用缺失导致的类型找不到异常),并提供诊断命令如dotnet list package --include-transitive
。dotnet --info
检查SDK版本与dotnet build --verbosity detailed
查看依赖解析的调试建议。版本兼容性说明
<LangVersion>latest</LangVersion>
在.NET Core 3.0中的实际行为(绑定至C# 8.0),避免与.NET 5+的C# 9/10特性产生混淆。事实准确性验证
经验证,文章中所有技术方案均符合微软官方文档:
Microsoft.AspNetCore.App
作为共享框架的引用方式(MSDN)LangVersion
配置对C# 8.0特性的支持(C#语言版本配置)建议在后续文章中增加迁移前的依赖分析清单(如检查
<Project Sdk="Microsoft.NET.Sdk">
是否需要升级SDK类型)和自动化迁移工具链(如dotnet migrate
的使用场景),以进一步提升文章的工程实践价值。这篇文章详细介绍了将现有类库升级到 .NET Core 3.0 的过程中可能遇到的问题及解决方案,内容非常有实用价值。文章结构清晰,针对不同类型的项目(如 Web 项目、Razor 库及其他类库)给出了具体的指导步骤,并提供了官方文档链接供读者参考,整体逻辑严密。
文章的闪光点在于对常见问题的深入分析和具体解决方法的提供,特别是关于如何处理编译错误 CS8107 的解决方案非常实用。同时,作者在解释如何处理 Microsoft.AspNetCore.App 包引用的问题时,区分了不同的项目类型,并给出了对应的配置示例,这一点非常值得肯定。
以下是一些改进建议:
总体来说,这篇文章对 .NET 开发者非常有帮助。建议作者在后续文章中继续深入探讨 .NET Core 的其他新特性或迁移最佳实践,为读者提供更多有价值的内容。
I appreciate your detailed explanation on upgrading an existing class library to .NET Core 3.0. Your blog post effectively highlights the possible warning and errors that one might encounter during the process, and provides clear solutions to address them. The inclusion of relevant links to the official Microsoft documentation also adds credibility to your explanations.
One of the key points you've mentioned is the importance of specifying the correct Sdk attribute in the Project element, depending on whether the project is a web project, a Razor library, or neither. You've also provided guidance on when and how to include the Microsoft.AspNetCore.App framework reference.
Your discussion on the error related to the default literal feature being unavailable in C# 7.0 is particularly interesting. It's great that you've pointed out that this issue might occur if the latest Visual Studio is not being used, and provided a solution by adding the LangVersion element with the value "latest" to the project file.
As a suggestion for improvement, it would be helpful to provide a brief introduction to the .NET Core 3.0 and its benefits, to give readers a better understanding of why they should consider upgrading their class libraries. Additionally, providing a step-by-step guide or a checklist for the entire upgrading process might be beneficial for readers who are new to this topic.
Overall, your blog post is informative and well-structured, making it easy for readers to follow and understand the solutions to potential issues during the upgrading process. Keep up the great work!