Azure Functions v4迁移调试报错:无法加载指定程序集
问题背景
已完成Azure Function从v2到v4的迁移,项目升级至.NET6,修复了包引用编译错误,解决方案包含3个适配.NET6和v4的项目,但调试时抛出System.IO.FileNotFoundException,提示无法加载Microsoft.Extensions.Configuration.Abstractions, Version=7.0.0.0。
项目配置
Azure Function项目
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Azure.Messaging.ServiceBus" Version="7.11.1" /> <PackageReference Include="Azure.Messaging.EventHubs" Version="5.7.5" /> <PackageReference Include="DocumentFormat.OpenXml" Version="2.18.0" /> <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.33" /> <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="5.1.2" /> <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.8.1" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.0" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" /> <PackageReference Include="Microsoft.Identity.Client" Version="4.48.1" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\PCM.1.z\x.1.z.csproj" /> </ItemGroup> <ItemGroup> <Compile Update="2.cs"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Compile> <Compile Update="1.cs"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Compile> <Compile Update="3.cs"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Compile> </ItemGroup> <ItemGroup> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> </ItemGroup> </Project>
第二个项目
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Azure.Messaging.ServiceBus" Version="7.11.1" /> <PackageReference Include="Azure.Messaging.EventHubs" Version="5.7.5" /> <PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" /> <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" /> <PackageReference Include="Microsoft.Azure.Devices" Version="1.38.2" /> <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.33" /> <PackageReference Include="SendGrid" Version="9.28.1" /> <PackageReference Include="StackExchange.Redis" Version="2.6.80" /> <PackageReference Include="Twilio" Version="6.0.1" /> <PackageReference Include="UnitsNet" Version="4.149.0" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\x.1.w\x.1.w.csproj" /> </ItemGroup> </Project>
第三个项目
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" /> </ItemGroup> </Project>
异常信息
Exception thrown: 'System.IO.FileNotFoundException' in Microsoft.Azure.WebJobs.Host.dll.
An exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.Azure.WebJobs.Host.dll but was not handled in user code
Could not load file or assembly 'Microsoft.Extensions.Configuration.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
修复方案
1. 统一Microsoft.Extensions系列包版本
Microsoft.NET.Sdk.Functions 4.1.3依赖Microsoft.Extensions.Configuration.Abstractions 6.x版本,当前项目中使用的7.0.0版本不兼容,需降级调整:
- 将Azure Function项目中的
Microsoft.Extensions.Configuration从7.0.0改为6.0.1 - 将
Microsoft.Extensions.Azure从1.6.0改为1.5.1(适配.NET6,依赖6.x的Extensions包)
2. 降级Entity Framework Core版本
.NET6需搭配EF Core 6.x版本,将第三个项目中的Microsoft.EntityFrameworkCore.SqlServer从7.0.0改为6.0.25:
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.25" />
3. 清理重建项目
- 删除所有项目的
bin和obj目录 - 执行
dotnet restore重新还原依赖包 - 重新编译并启动调试
4. 强制统一依赖版本
若仍存在版本冲突,在Azure Function项目的<ItemGroup>中添加直接引用,强制指定版本:
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.1" />
5. 验证本地工具版本
确保本地Azure Functions Core Tools为v4版本,执行func --version查看,若版本过低则升级至最新v4版本。
内容的提问来源于stack exchange,提问作者Sammy

