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

.NET 6 Azure Function使用JsonSerializer反序列化时程序集加载失败求助

解决.NET 6 Azure Function中System.Text.Json版本不匹配问题

问题场景

我用.NET 6开发了一个Azure Function,核心代码如下:

[FunctionName("CarCounterFunc")]
public void Run(
    [BlobTrigger("input/{name}.xml")] Stream inputBlob,IBinder binder,string
    name,ILogger log)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(inputBlob);

    string json = jsonHandling.Convert(doc);
    json = jsonHandling.Clean(json);

    List<CarCounter> myList = JsonSerializer.Deserialize<List<CarCounter>>(json);
}

添加List<CarCounter> myList = JsonSerializer.Deserialize<List<CarCounter>>(json);这行代码后,出现以下错误:

[2023-05-15T16:07:48.680Z] Executing 'CarCounterFunc' (Reason='New blob detected(LogsAndContainerScan): input/a9909523-a23b-47ed-9732-40ea4535c82f.xml', Id=68bf0483-5f36-4325-8d82-01e02c0b3361)
[2023-05-15T16:07:48.689Z] Trigger Details: MessageId: b427a04e-620b-44db-8948-d5d9cc25145a, DequeueCount: 1, InsertedOn: 2023-05-15T16:07:47.000+00:00, BlobCreated: 2023-05-15T16:07:46.000+00:00, BlobLastModified: 2023-05-15T16:07:46.000+00:00
[2023-05-15T16:07:48.955Z] Executed 'CarCounterFunc' (Failed, Id=68bf0483-5f36-4325-8d82-01e02c0b3361, Duration=627ms)
[2023-05-15T16:07:48.957Z] System.Private.CoreLib: Exception while executing function: CarCounterFunc. CarCounterprj: Could not load file or assembly 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Det går inte att hitta filen.

解决方案

1. 统一System.Text.Json版本为.NET 6兼容版本

.NET 6默认自带的System.Text.Json版本是6.x,报错显示程序试图加载7.0.0.0版本,说明项目中某个依赖引用了更高版本的包:

  • 打开项目的NuGet包管理器,找到System.Text.Json包,将版本降级到6.x系列(比如6.0.25)。
  • 若通过csproj文件管理,直接修改包引用:
<PackageReference Include="System.Text.Json" Version="6.0.25" />

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

如果无法降级依赖,可添加绑定重定向让运行时自动适配版本:

  • 隔离进程模型:在项目根目录创建function.proj文件,加入以下内容:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.14.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Blobs" Version="5.0.0" />
    <PackageReference Include="System.Text.Json" Version="6.0.25" />
  </ItemGroup>
  <ItemGroup>
    <RuntimeHostConfigurationOption Include="Microsoft.NET.Sdk.Functions.UseWorkers" Value="true" />
  </ItemGroup>
  <ItemGroup>
    <AssemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </AssemblyBinding>
  </ItemGroup>
</Project>
  • In-process模型:将绑定重定向配置添加到app.config或web.config中。

3. 检查并修复依赖链

使用NuGet包管理器的「依赖项」视图,查看哪个引用间接引入了System.Text.Json 7.x版本,找到后将该依赖更新为兼容.NET 6的版本,从根源避免版本冲突。

4. 清理重建并重新部署

执行以下命令清理项目并重新还原依赖:

dotnet clean
dotnet restore

重新编译后部署Azure Function到云端,确保所有依赖包正确上传。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 11:05:28