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

.NET 8 Azure Function程序集加载错误解决及隔离模式咨询

问题描述

近期将Azure Function App从.NET 6升级至.NET 8,本地编译成功,但运行时触发程序集加载错误:

function is in error: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=8.0.0.0'

调研后确认该问题与Microsoft.Extensions.Logging.Abstractions相关,属于.NET 8常见问题。由于依赖的其他项目已升级至.NET 8,无法回退到.NET 6。考虑切换到Azure Functions的Isolated Mode(隔离模式),但不确定可行性及实施步骤,当前项目的.csproj文件如下:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <DockerFastModeProjectMountDirectory>/home/site/wwwroot</DockerFastModeProjectMountDirectory>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Azure.Identity" Version="1.10.4" />
    <PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.6.0" />
    <PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
    <PackageReference Include="Azure.Storage.Files.Shares" Version="12.17.1" />
    <PackageReference Include="Azure.Storage.Queues" Version="12.17.1" />
    <PackageReference Include="Microsoft.AspNetCore.AzureKeyVault.HostingStartup" Version="2.0.4" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.2.2" />
    <PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.2" />
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.3.0" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
    <PackageReference Include="SendGrid" Version="9.28.1" />
  </ItemGroup>
  <ItemGroup>
    <!--These Projects are now .net 8.0-->
    <ProjectReference Include="..\AsoRockFood.com.Umbraco\Models\AsoRockFood.com.Umbraco.Models.csproj" />
    <ProjectReference Include="..\RockLife.Services\AsoRockFood.com.Services.csproj" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>
解决方案

方案一:不切换隔离模式,修复程序集加载问题

  1. 更新Microsoft.NET.Sdk.Functions包
    当前使用的4.3.0版本对.NET 8支持不完善,升级到最新稳定版(至少4.2.0以上,推荐最新v4.x版本):
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.28.1" /> <!-- 替换为最新版本 -->
    
  2. 添加程序集绑定重定向
    在项目根目录创建app.config文件(无则新建),添加以下内容强制统一程序集版本:
    <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    
  3. 清理重建项目
    • 执行命令:dotnet clean && dotnet restore && dotnet build
    • 删除本地bin、obj文件夹后重新生成

方案二:切换到隔离模式(Isolated Mode)

隔离模式下函数运行在独立.NET进程中,彻底避免与宿主程序集版本冲突,完全适配.NET 8,是长期最优解。实施步骤如下:

1. 修改.csproj文件

  • 替换SDK为Microsoft.NET.Sdk.Functions.Worker
  • 添加OutputType为Exe
  • 更新包引用,移除旧的Microsoft.NET.Sdk.Functions,添加隔离模式核心包:
    <Project Sdk="Microsoft.NET.Sdk.Functions.Worker">
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <AzureFunctionsVersion>v4</AzureFunctionsVersion>
        <OutputType>Exe</OutputType>
        <DockerFastModeProjectMountDirectory>/home/site/wwwroot</DockerFastModeProjectMountDirectory>
        <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
        <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Azure.Identity" Version="1.10.4" />
        <PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.6.0" />
        <PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
        <PackageReference Include="Azure.Storage.Files.Shares" Version="12.17.1" />
        <PackageReference Include="Azure.Storage.Queues" Version="12.17.1" />
        <!-- 移除旧的Microsoft.AspNetCore.AzureKeyVault.HostingStartup,隔离模式使用新配置方式 -->
        <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" /> <!-- 最新稳定版 -->
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" /> <!-- 最新稳定版 -->
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="5.2.2" /> <!-- 对应存储扩展 -->
        <PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.2" />
        <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
        <PackageReference Include="SendGrid" Version="9.28.1" />
      </ItemGroup>
      <ItemGroup>
        <!--These Projects are now .net 8.0-->
        <ProjectReference Include="..\AsoRockFood.com.Umbraco\Models\AsoRockFood.com.Umbraco.Models.csproj" />
        <ProjectReference Include="..\RockLife.Services\AsoRockFood.com.Services.csproj" />
      </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
    </Project>
    

2. 更新host.json

添加隔离模式专属配置:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensions": {
    "http": {
      "routePrefix": ""
    }
  },
  "functionTimeout": "00:10:00"
}

3. 修改函数代码结构

隔离模式函数写法调整,以HTTP触发函数为例:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

public class MyFunction
{
    private readonly ILogger<MyFunction> _logger;

    public MyFunction(ILogger<MyFunction> logger)
    {
        _logger = logger;
    }

    [Function("MyHttpFunction")]
    public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
    {
        _logger.LogInformation("C# HTTP trigger function processed a request.");

        var response = req.CreateResponse(System.Net.HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
        response.WriteString("Welcome to Azure Functions!");

        return response;
    }
}

4. 添加启动类(可选,用于依赖注入配置)

如需自定义依赖注入或服务配置,创建Program.cs:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(services =>
    {
        // 添加自定义服务配置,比如Azure客户端、数据库连接等
        services.AddAzureClients(clientBuilder =>
        {
            clientBuilder.AddSecretClient(new Uri("your-keyvault-uri"));
            clientBuilder.AddBlobServiceClient("your-storage-connection-string");
        });
    })
    .Build();

host.Run();

5. 清理测试

  • 删除本地bin、obj文件夹
  • 执行dotnet restore && dotnet build
  • 用func start启动本地测试

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 22:44:51