Maui Blazor 中文社区 QQ群:645660665

升级 Net 7 随手笔记 (注意事项以及解决方案) - 持续更新

  1. 条件编译 #if NET6_0 改为 #if NET7_0 或者 #if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER
using BootstrapBlazor.Components;
#endif
  1. 项目目标支持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> 
  1. 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>
  1. 多目标 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> 
  1. BrotliCompressionProviderOptions 提示 (CompressionLevel)4 不正确
            builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
            {
                //options.Level = (CompressionLevel)4;
                options.Level = CompressionLevel.Optimal; //改为这个
            });
  1. 库生成提示 'xxx.dll' does not contain an entry point. 项目文件 PropertyGroup 内加入 <OpenApiGenerateDocuments>false</OpenApiGenerateDocuments>

  2. 提示 证书链是由不受信任的颁发机构颁发的 , 链接串后加入 ;Encrypt=False

  3. 有编辑过 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 //这句是我项目另外加的
    }
  }
}
  1. 库升级 .Net 7.0 后生成提示 Assembly "xxx.dll" does not contain an entrypoint 临时解决办法
    在工程项目文件.csproj 添加这行
<PropertyGroup>
  ...
  <OpenApiGenerateDocuments>false</OpenApiGenerateDocuments>
  ...
</PropertyGroup>

就可以跳过 GenerateOpenApiDocuments 继续生成了

  1. 升级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"
  }
}
  1. 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();
posted @   AlexChow  阅读(1563)  评论(11编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek-R1本地部署如何选择适合你的版本?看这里
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
· 我们是如何解决abp身上的几个痛点
· 普通人也能轻松掌握的20个DeepSeek高频提示词(2025版)
点击右上角即可分享
微信分享提示