.NET Core 3.1 Azure Functions文件存在却无法加载System.Text.Encodings.Web
问题排查与解决方案
根因
Azure Functions v3运行时默认内置了低版本的System.Text.Encodings.Web、System.Text.Json程序集,函数宿主启动时会优先加载内置版本,导致项目引用的6.0版本无法被正常识别加载,即使输出目录存在匹配的程序集文件也会抛出找不到文件的异常。
可选解决方法
- 方法1:降级System.Text系列包版本
将项目文件中两个相关包的版本降级到与.NET Core 3.1匹配的3.1.x系列,无需额外配置即可解决版本冲突:
<PackageReference Include="System.Text.Encodings.Web" Version="3.1.32" /> <PackageReference Include="System.Text.Json" Version="3.1.32" />
修改后清理并重新生成项目即可。
- 方法2:添加程序集重定向规则(需保留6.0版本时使用)
在项目文件的PropertyGroup节点下添加自动生成绑定重定向的配置:
<PropertyGroup> <!-- 原有配置保持不变,新增以下两行 --> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> </PropertyGroup>
如果添加后仍报错,可在项目根目录新增app.config文件,写入手动绑定重定向规则:
<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Text.Encodings.Web" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
- 方法3:升级Azure Functions版本
直接将项目升级为Azure Functions v4,目标框架改为.NET 6,原生支持6.0版本的System.Text相关包,无需额外处理版本冲突问题。
内容的提问来源于stack exchange,提问作者YMC
相关产品推荐
相关产品推荐

