升级 Net 7 随手笔记 (注意事项以及解决方案) - 持续更新
- 条件编译 #if NET6_0 改为 #if NET7_0 或者 #if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER
using BootstrapBlazor.Components;
#endif
-
项目目标支持6和7改为
<TargetFramework>net6.0;net7.0</TargetFrameworks>
,只需要7直接改为<TargetFramework>net7.0</TargetFramework>
多目标引用库参考
<ItemGroup Condition="'$(TargetFramework)' != 'net7.0'">
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="6.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="7.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.10" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0" />
</ItemGroup>
- Maui工程或者库参考目标方案
<TargetFrameworks>netstandard2.0;netstandard2.1;net461;net6.0;net6.0-windows10.0.19041;net6.0-ios;net6.0-maccatalyst;net6.0-android;net7.0;net7.0-windows10.0.19041;net7.0-ios;net7.0-maccatalyst;net7.0-android</TargetFrameworks>
- 多目标 net461;net7.0 提示冲突 System.Drawing 存在于 4.0 和 7.0
<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="7.0" />
</ItemGroup>
- BrotliCompressionProviderOptions 提示 (CompressionLevel)4 不正确
builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
{
//options.Level = (CompressionLevel)4;
options.Level = CompressionLevel.Optimal; //改为这个
});
-
库生成提示 'xxx.dll' does not contain an entry point. 项目文件
PropertyGroup
内加入<OpenApiGenerateDocuments>false</OpenApiGenerateDocuments>
-
提示
证书链是由不受信任的颁发机构颁发的
, 链接串后加入;Encrypt=False
-
有编辑过
xxx.runtimeconfig.json
文件的同学注意了: 升级了net7,这个文件也要相应的更新. 例如本人就加过这句
"System.Drawing.EnableUnixSupport": true
并且生成 release 特定跳过了复制这个文件 xxx.runtimeconfig.json
倒置发布到 centos后服务无法启动,一直提示
Unhandled exception. System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
The located assembly's manifest definition does not match the assembly reference. (0x80131040)
File name: 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
排查了半天才想起这个文件. 新版默认文件内容为
{
"runtimeOptions": {
"tfm": "net7.0",
"frameworks": [
{
"name": "Microsoft.NETCore.App",
"version": "7.0.0"
},
{
"name": "Microsoft.AspNetCore.App",
"version": "7.0.0"
}
],
"configProperties": {
"System.GC.Server": true,
"System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
"System.Reflection.NullabilityInfoContext.IsSupported": true,
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false,
"System.Drawing.EnableUnixSupport": true //这句是我项目另外加的
}
}
}
- 库升级 .Net 7.0 后生成提示 Assembly "xxx.dll" does not contain an entrypoint 临时解决办法
在工程项目文件.csproj 添加这行
<PropertyGroup>
...
<OpenApiGenerateDocuments>false</OpenApiGenerateDocuments>
...
</PropertyGroup>
就可以跳过 GenerateOpenApiDocuments 继续生成了
- 升级vs或者装了.Net 7.0后, 工程框架用 net6 的 dotnet watch 出错 'Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=7.0.0.0'
工程框架
<TargetFramework>net6.0</TargetFramework>
错误
dotnet watch 🚀 Started
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at System.Reflection.RuntimeAssembly.GetType(QCallAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type, ObjectHandleOnStack keepAlive, ObjectHandleOnStack assemblyLoadContext)
at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at System.Reflection.Assembly.GetType(String name, Boolean throwOnError)
at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)
at System.StartupHookProvider.ProcessStartupHooks()
临时解决方案: 工程目录下建立 global.json 文件指定编译框架
{
"sdk": {
"version": "6.0.403"
}
}
- Blazor/ASP.Net core 提示警告 ASP0014:建议使用顶级路由注册 'ASP0014: Suggest using top level route registrations'
原因:
路由可以直接在最小 API 应用程序的顶层注册。
解决:
路由可以直接在最小 API 应用程序的顶层注册,不需要嵌套在 UseEndpoints 调用中
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
endpoints.MapControllerRoute("mvc", "{controller}/{action}");
endpoints.MapControllerRoute("mvc2", "api/{controller}/{action}");
});
app.Run();
改为
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.MapControllerRoute("mvc", "{controller}/{action}");
app.MapControllerRoute("mvc2", "api/{controller}/{action}");
app.Run();
关联项目
FreeSql QQ群:4336577
BA & Blazor QQ群:795206915
Maui Blazor 中文社区 QQ群:645660665
知识共享许可协议
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名AlexChow(包含链接: https://github.com/densen2014 ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系 。
转载声明
本文来自博客园,作者:周创琳 AlexChow,转载请注明原文链接:https://www.cnblogs.com/densen2014/p/16882636.html
AlexChow
今日头条 | 博客园 | 知乎 | Gitee | GitHub
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek-R1本地部署如何选择适合你的版本?看这里
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
· 我们是如何解决abp身上的几个痛点
· 普通人也能轻松掌握的20个DeepSeek高频提示词(2025版)