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

将.NET Functions App升级至.NET 7引发程序集加载错误

解决Azure Functions升级.NET7后的程序集加载错误

问题原因分析

  1. Microsoft.AspNetCore.Mvc.Abstractions包版本误区:.NET Core 3.0及以后版本中,该程序集已被整合到ASP.NET Core共享框架中,不再以独立NuGet包形式发布——这就是你在NuGet上只能找到2.2.0版本的原因。项目运行时寻找7.0.0.0版本,是因为.NET7的共享框架包含该程序集的7.0版本,但Azure Functions运行时未正确加载共享框架中的程序集。
  2. Functions运行时兼容性:Azure Functions对.NET7的支持依赖特定的运行时版本和宿主模式(隔离/进程内),若配置不匹配会导致程序集加载异常。

解决方案步骤

1. 确认Azure Functions项目配置

检查项目文件(.csproj)中的核心配置:

<!-- 隔离模式(推荐.NET7使用) -->
<TargetFramework>net7.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<!-- 必须引用的隔离模式SDK包 -->
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.14.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.10.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
  • 若使用进程内模式,需确保Azure Functions Core Tools已更新至支持.NET7的最新版本(v4.x),同时检查是否有过时的包引用导致冲突。

2. 添加程序集绑定重定向

针对进程内模式项目,在app.config中添加绑定重定向,强制使用.NET7版本的程序集:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Linq.Expressions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.AspNetCore.Mvc.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
  • 隔离模式项目无需手动添加,确保GenerateRuntimeConfigurationFiles设置为true,让SDK自动生成正确的运行时配置:
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    

3. 清理重建项目

  • 删除项目根目录下的bin、obj文件夹;
  • 执行命令清除NuGet缓存:
    dotnet nuget locals all --clear
    
  • 重新恢复依赖并构建:
    dotnet restore
    dotnet build
    

4. 排查第三方依赖冲突

使用命令查看项目所有依赖,确认是否有第三方包引用了旧版本的目标程序集:

dotnet list package

若发现冲突包,尝试升级该包至兼容.NET7的版本,或通过<PackageReference>的NoWarn属性抑制版本警告。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 16:01:47