Azure Function v1程序集加载失败及包版本冲突问题求助
针对你遇到的System.Threading.Tasks.Extensions和Newtonsoft.Json的问题,我结合Azure Function v1的特性给出具体的修复方案:
一、修复System.Threading.Tasks.Extensions, Version=4.2.0.0缺失问题
问题根源分析
当依赖项目目标为.NET Standard 2.0时,System.Threading.Tasks.Extensions属于.NET Standard 2.0的框架兼容集,不会作为独立DLL输出;而目标为.NET 4.7.2时会生成独立DLL。但Azure Function v1的构建/部署流程没有自动将这个依赖从.NET Standard项目传递复制到函数的bin目录,即使deps.json有记录。
具体修复步骤
直接在Azure Function项目中安装对应NuGet包
打开NuGet包管理器,为你的Azure Function v1项目安装System.Threading.Tasks.Extensions包,选择与程序集版本4.2.0.0匹配的NuGet版本(对应NuGet版本为4.5.2)。安装完成后,右键该引用,确保Copy Local设置为True。强制复制依赖到输出目录
如果上述步骤无效,可以手动编辑Azure Function项目的.csproj文件,添加以下代码,确保该DLL被强制复制:<ItemGroup> <PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.2" /> <Reference Include="System.Threading.Tasks.Extensions"> <HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath> <Private>True</Private> </Reference> </ItemGroup>(注意调整
HintPath为你本地NuGet包的实际路径)检查部署配置
如果你使用发布工具部署Azure Function,确保在发布设置中勾选了Include all files from the bin directory选项,避免部署时遗漏依赖文件。
二、修复Newtonsoft.Json版本冲突问题
问题根源分析
Azure Function v1默认依赖特定版本的Newtonsoft.Json(通常是v10.x),但你的依赖项目使用了不同版本(v12.x),导致程序集加载时版本不匹配。
具体修复步骤
统一版本并添加绑定重定向
- 首先,确保所有项目(包括Azure Function和依赖项目)安装相同版本的Newtonsoft.Json(推荐选择与Azure Function兼容的最高版本,或者根据你的需求统一为v12.x)。
- 然后,在Azure Function项目的
web.config(或app.config)中添加绑定重定向规则,强制所有版本请求指向你使用的版本:
把<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>newVersion替换为你实际使用的版本号。
禁用Azure Function的默认Newtonsoft.Json引用
如果绑定重定向无效,可以在Azure Function项目的.csproj文件中添加以下代码,排除默认的框架引用:<ItemGroup> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <Reference Remove="Newtonsoft.Json" /> </ItemGroup>确保你的NuGet包版本覆盖了框架默认的引用。
额外注意事项
- Azure Function v1是基于.NET Framework的旧版本,对.NET Standard依赖的兼容性有限,尽量让依赖项目优先使用
.NET 4.7.2作为目标框架,减少跨框架依赖带来的问题。 - 每次修改依赖后,清理解决方案并重新构建,避免旧的缓存文件干扰。
内容的提问来源于stack exchange,提问作者Benjamin E.

