You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Host.CreateDefaultBuilder()时遇类型加载异常(.NET 8 Preview 7)

问题描述

运行.NET 8 Preview 7时,调用Host.CreateDefaultBuilder()持续抛出以下错误:

错误信息

The type initializer for 'Microsoft.Extensions.Logging.EventSource.LoggingEventSource' threw an exception

堆栈跟踪

Could not load type 'System.Runtime.CompilerServices.NullableContextAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at System.ModuleHandle.ResolveType(QCallModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctorWithParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
   at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat, StringBuilder sb)
   at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat)
   at System.Exception.get_StackTrace()

请问遗漏了什么配置或依赖?


补充说明

项目采用CQRS架构,各csproj文件内容如下:

入口应用csproj

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

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>
    
    <ItemGroup>
      <PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
      <PackageReference Include="Serilog.AspNetCore.Enrichers.CorrelationId" Version="1.0.0" />
      <PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
      <PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="..\..\..\Jetpack.Extensions.Hosting\src\Jetpack.Extensions.Hosting\Jetpack.Extensions.Hosting.csproj" />
        <ProjectReference Include="..\CQRS.Application\CQRS.Application.csproj" />
        <ProjectReference Include="..\CQRS.Infrastructure\CQRS.Infrastructure.csproj" />
        <ProjectReference Include="..\CQRS.Persistence\CQRS.Persistence.csproj" />
    </ItemGroup>

    <ItemGroup>
        <None Update="appsettings.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
    </ItemGroup>

</Project>

Application csproj

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

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
      <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="8.0.0-preview.4" />
      <PackageReference Include="Twilio" Version="6.11.0" />
      <PackageReference Include="WolverineFx" Version="1.5.0" />
      <PackageReference Include="WolverineFx.FluentValidation" Version="1.5.0" />
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..\CQRS.Domain\CQRS.Domain.csproj" />
    </ItemGroup>

</Project>

Domain csproj

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

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <Folder Include="Enums\" />
    </ItemGroup>

    <ItemGroup>
      <PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="8.0.0-preview.7.23375.9" />
      <PackageReference Include="NodaTime" Version="3.1.9" />
    </ItemGroup>

</Project>

Infrastructure csproj

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

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <ProjectReference Include="..\CQRS.Application\CQRS.Application.csproj" />
    </ItemGroup>


    <ItemGroup>
      <PackageReference Include="Flurl.Http" Version="4.0.0-pre3" />
      <PackageReference Include="MailKit" Version="4.1.0" />
      <PackageReference Include="SendGrid" Version="9.28.1" />
      <PackageReference Include="Stripe.net" Version="41.27.0" />
      <PackageReference Include="Twilio" Version="6.11.0" />
    </ItemGroup>
</Project>

Persistence csproj

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

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <WarningsAsErrors>Nullable</WarningsAsErrors>
    </PropertyGroup>

    <ItemGroup>
      <ProjectReference Include="..\CQRS.Application\CQRS.Application.csproj" />
    </ItemGroup>

    <ItemGroup>
      <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
      <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0-preview.7.23375.9" />
      <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="8.0.0-preview.4" />
    </ItemGroup>
    
</Project>

解决方案

这个问题核心是程序集版本不兼容,具体修复步骤如下:

1. 修复Identity组件版本冲突

在Persistence项目中,你同时引用了Microsoft.AspNetCore.Identity 2.2.0(旧版本)和.NET 8 Preview 7版本的Microsoft.AspNetCore.Identity.EntityFrameworkCore。旧版Identity依赖的System.Runtime程序集与.NET 8中NullableContextAttribute的定义不匹配,导致加载失败。

修复操作:
将Persistence项目中的Microsoft.AspNetCore.Identity包版本升级到与Microsoft.AspNetCore.Identity.EntityFrameworkCore一致的8.0.0-preview.7.23375.9:

<PackageReference Include="Microsoft.AspNetCore.Identity" Version="8.0.0-preview.7.23375.9" />

2. 统一预览版NuGet包版本

当前项目中存在预览包版本不一致的情况:

  • Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime使用8.0.0-preview.4
  • Identity相关包使用8.0.0-preview.7

版本差异会引发依赖链冲突,建议将所有.NET 8预览版包统一升级到Preview 7版本:

  • 若Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime已发布Preview 7版本,直接升级;若未发布,需确认其与.NET 8 Preview 7的兼容性,必要时暂时保留但密切关注后续更新。

3. 清理重建项目

完成版本调整后,执行以下操作确保依赖完全更新:

  • 删除所有项目的bin和obj文件夹
  • 运行命令:dotnet clean 和 dotnet restore
  • 重新编译项目

4. 检查自定义Host项目依赖

你引用了自定义的Jetpack.Extensions.Hosting项目,需确保该项目也以.NET 8 Preview 7为目标框架,且依赖的Microsoft.Extensions.*包版本与主项目一致,避免引入旧版程序集。


内容的提问来源于stack exchange,提问作者Dblock247

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 00:29:52