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

Azure Functions升级至.NET7遇System.Linq加载失败问题求助

问题描述

将Azure Functions应用升级到.NET 7后,启动阶段出现如下运行时错误:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Linq, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'

我使用FunctionsStartup与IFunctionsHostBuilder接口,因用到自定义Binding类型,通过AddWebJobs扩展注册扩展提供器,代码如下:

var webJobsBuilder = builder.Services.AddWebJobs(x => { return; });
webJobsBuilder.AddExtension<AccessTokenExtensionProvider>();

执行到此处时失败。该代码在.NET 6环境下运行正常,想知道如何解决这个问题?能否不使用WebJobs实现需求?

最初我用WebJobsStartup而非FunctionsStartup,但出现了相同的错误,我不确定System.Linq 7.0.0.0是否真实存在。

项目文件内容

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <UserSecretsId>34849d02-37f1-4724-b383-4d0896da65e4</UserSecretsId>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Azure.Data.Tables" Version="12.6.1" />
    <PackageReference Include="Azure.Storage.Files.Shares" Version="12.12.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Core" Version="1.8.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.10" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="5.1.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.8.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Host.Storage" Version="4.1.0" />
    <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="5.1.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
    <PackageReference Include="Microsoft.IdentityModel.Protocols" Version="6.25.0" />
    <PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.25.0" />
    <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.25.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Properties\PublishProfiles\" />
  </ItemGroup>
</Project>

Startup文件代码

using System.Linq;
using SampleProject.Portal.Api;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SampleProject.Portal.Api.B2C;

[assembly: FunctionsStartup(typeof(Startup))]
namespace SampleProject.Portal.Api;

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        ServiceDescriptor logger = builder.Services.FirstOrDefault(s => s.ServiceType == typeof(ILogger<>));

        if (logger != null)
        {
            builder.Services.Remove(logger);
        }

        builder.AddAccessTokenBinding();
    }
}

解决方案

1. 修复System.Linq版本依赖问题

首先明确:System.Linq 7.0.0.0并不存在。.NET 7中System.Linq作为框架核心库的一部分,不会以独立的7.0.0.0版本程序集形式存在,这个错误是依赖版本冲突或绑定重定向配置缺失导致的。

  • 添加自动绑定重定向配置:
    在项目文件的<PropertyGroup>节点下添加以下配置,自动生成正确的程序集绑定重定向,将对7.0.0.0版本的请求重定向到实际存在的版本:

    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    
  • 更新兼容的NuGet包:
    将Microsoft.NET.Sdk.Functions升级到最新的v4版本(当前推荐4.2.0及以上),同时检查所有WebJobs相关扩展包(如Microsoft.Azure.WebJobs.Extensions.*),升级到兼容.NET 7的版本,例如:

    • Microsoft.Azure.WebJobs.Extensions.Storage升级到5.2.0+
    • Microsoft.Azure.WebJobs.Extensions.CosmosDB升级到4.0.0+

2. 不使用WebJobs注册自定义Binding

在Azure Functions v4 + .NET 7环境下,注册自定义Binding扩展可以直接通过IFunctionsHostBuilder完成,无需手动调用AddWebJobs:

修改AddAccessTokenBinding扩展方法的实现,直接通过builder.Services注册服务:

public static class FunctionsHostBuilderExtensions
{
    public static IFunctionsHostBuilder AddAccessTokenBinding(this IFunctionsHostBuilder builder)
    {
        // 直接注册扩展提供器,无需依赖WebJobsBuilder
        builder.Services.AddSingleton<IExtensionConfigProvider, AccessTokenExtensionProvider>();
        // 按需注册其他相关服务
        return builder;
    }
}

这种方式符合Azure Functions v4的依赖注入规范,能避免手动创建WebJobsBuilder带来的依赖冲突。

3. 清理重建项目

  • 删除项目的bin和obj目录,清除缓存的旧程序集。
  • 执行dotnet restore重新还原依赖,再执行dotnet build重新构建项目。

额外注意事项

  • 不要混合使用WebJobsStartup和FunctionsStartup,Azure Functions v4中推荐使用FunctionsStartup进行依赖注入配置。
  • 检查项目是否引用了旧版本的.NET Standard或.NET Framework库,这类库可能引发程序集版本冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 12:30:55