.NET 8 Azure Functions部署到Azure后加载System.Text.Json失败求助
.NET 8 Azure Functions部署后System.Text.Json加载失败问题解决
问题描述
基于.NET 8开发的Azure Functions本地运行正常,但部署到Azure后无法启动,Application Insights报错:
无法加载文件或程序集'System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51',系统找不到指定文件。
项目配置如下:
.csproj文件
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <AzureFunctionsVersion>V4</AzureFunctionsVersion> <RootNamespace>azure_function</RootNamespace> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.Azure.Devices" Version="1.39.1" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.4.1" /> <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" /> <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.4.0" /> <PackageReference Include="System.Text.Json" Version="8.0.4" /> </ItemGroup> <ItemGroup> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> </ItemGroup> </Project>
local.settings.json文件
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "", "FUNCTIONS_INPROC_NET8_ENABLED": "1", "FUNCTIONS_WORKER_RUNTIME": "dotnet" } }
问题原因
- 运行模型冲突:同时引用了In-Process模型的
Microsoft.NET.Sdk.Functions和Isolated Worker模型的Microsoft.Azure.Functions.Worker类库,导致依赖加载逻辑混乱。 - 版本绑定问题:手动引用的
System.Text.Json 8.0.4与运行环境期望的8.0.0.0版本不匹配,且缺少绑定重定向配置。
解决步骤
1. 统一函数运行模型
Azure Functions的In-Process和Isolated Worker模型不可混用,需二选一调整配置:
选项A:保留In-Process模型(对应local.settings.json的FUNCTIONS_INPROC_NET8_ENABLED=1)
移除Isolated Worker相关的包引用:
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" /> <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.4.0" />
建议升级Microsoft.NET.Sdk.Functions到.NET 8兼容的最新版本(如4.2.0+)。
选项B:切换到Isolated Worker模型
- 修改.csproj的Sdk声明:
<Project Sdk="Microsoft.NET.Sdk.Functions.Worker"> - 移除In-Process模型的包引用:
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.4.1" /> - 更新local.settings.json:
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "", "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated" } }
2. 处理System.Text.Json依赖
- In-Process模型:.NET 8已内置System.Text.Json,可直接移除手动添加的
System.Text.Json包引用,由SDK自动管理依赖版本。 - 强制版本绑定:若必须保留手动引用,需添加程序集绑定重定向。在项目根目录创建
App.config文件,内容如下:
并在.csproj中添加配置,确保文件被复制到输出目录:<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-8.0.4.0" newVersion="8.0.4.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration><ItemGroup> <None Update="App.config"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> </ItemGroup>
3. 重新部署验证
- 清理项目:执行
dotnet clean - 构建发布:执行
dotnet publish -c Release - 重新部署到Azure Function App,检查Application Insights日志确认问题修复。
内容的提问来源于stack exchange,提问作者Jean J. Michel
相关产品推荐
相关产品推荐

