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

.NET Standard 2.0项目引用Serilog后.NET Framework 4.8调用找不到netstandard2.1报错

问题成因

你遇到的报错本质是依赖链上的包版本不兼容:

  • .NET Framework 4.8最高仅支持 .NET Standard 2.0,不支持.NET Standard 2.1
  • 你的日志类库没有给Serilog相关依赖指定版本,NuGet默认拉取了最新版的Serilog.AspNetCore,该包的4.x及以上版本依赖.NET Standard 2.1,连带引入了要求netstandard 2.1.0.0的Serilog核心程序集,所以运行时.NET Framework 4.8会尝试加载不存在的netstandard 2.1.0.0文件,触发缺失异常。
    .NET 6项目能正常运行是因为.NET Core 3.0+原生支持.NET Standard 2.1,不存在兼容问题。

解决步骤

  1. 限定日志类库的Serilog版本为兼容.NET Standard 2.0的版本
    修改你的.NET Standard 2.0日志项目的包引用,指定版本:
<ItemGroup>
    <PackageReference Include="Microsoft.Win32.Registry" Version="4.7.0" />
    <PackageReference Include="SchneiderElectric.Software.SASigning" />
    <!-- 指定Serilog版本,2.12.0为稳定支持.NET Standard 2.0的版本 -->
    <PackageReference Include="Serilog" Version="2.12.0" />
    <!-- 指定Serilog.AspNetCore版本,3.4.0是最后一个支持.NET Standard 2.0的版本 -->
    <PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
</ItemGroup>

如果你的日志类库只用到Serilog基础功能,不需要AspNetCore相关的中间件,可以直接移除Serilog.AspNetCore包,只保留Serilog核心包,进一步减少依赖冲突。

  1. 给.NET Framework 4.8项目开启自动绑定重定向
    修改.NET Framework 4.8项目的项目文件,在PropertyGroup节点添加如下配置:
<PropertyGroup>
    <!-- 原有配置保持不变 -->
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

如果上述配置后还是有绑定报错,可以手动在.NET Framework 4.8项目的App.config的runtime节点下添加netstandard的绑定重定向:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="netstandard" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
  1. 清理重建解决方案
    删除所有项目的bin、obj文件夹,完全清理NuGet缓存后重新编译整个解决方案,确保所有依赖都按指定版本加载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 23:45:03