Azure Function v4 .NET6本地加载Microsoft.Extensions.Http 7+失败
Azure Functions v4 .NET6 升级Microsoft.Extensions.Http到7.0+后加载失败的解决方法
问题场景
本地运行Azure Functions v4 .NET6项目,通过Microsoft.Extensions.Http注册HttpClient,代码及配置如下:
Startup代码
using DinersService.Helpers; using DinersService.Interfaces; using Microsoft.Azure.Functions.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection; [assembly: FunctionsStartup(typeof(DinersService.Startup))] namespace DinersService { public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { builder.Services.AddHttpClient(); builder.Services.AddScoped<IApiClient, ApiClient>(); } } }
.csproj配置
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" /> </ItemGroup> <ItemGroup> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> </ItemGroup> </Project>
使用Microsoft.Extensions.Http 6.0.0版本时运行正常,但升级到7.0.0及以上版本时,出现以下错误:
Error configuring services in an external startup class. DinersService: Could not load file or assembly 'Microsoft.Extensions.Http, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. [2023-12-13T10:19:39.349Z] A host error has occurred during startup operation '628e8d49-0172-4230-a0e3-861455a94086'. [2023-12-13T10:19:39.350Z] Microsoft.Azure.WebJobs.Script: Error configuring services in an external startup class. DinersService: Could not load file or assembly 'Microsoft.Extensions.Http, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. Value cannot be null. (Parameter 'provider')
解决方法
对齐依赖包版本到.NET6兼容范围
Azure Functions v4(基于.NET6)运行时依赖.NET6生态的Microsoft.Extensions系列组件,7.0+、8.0+版本的Microsoft.Extensions.Http和Microsoft.Extensions.DependencyInjection属于.NET7/.NET8版本,与.NET6运行时存在程序集版本冲突。需将这些包降级到.NET6的稳定版本,推荐使用6.0.16(.NET6的最新补丁版本)。更新Microsoft.NET.Sdk.Functions到兼容版本
当前使用的Microsoft.NET.Sdk.Functions4.2.0版本较旧,建议升级到与.NET6匹配的最新4.x稳定版本(如4.2.1或更高),确保运行时与依赖包的兼容性。清理编译缓存并重新生成
- 删除项目目录下的
bin和obj文件夹,彻底清除旧编译产物 - 执行
dotnet restore命令重新还原所有依赖 - 重新编译并启动项目
- 添加程序集绑定重定向(可选)
如果上述步骤仍无法解决版本冲突,可在项目根目录创建app.config文件,添加绑定重定向强制运行时使用兼容的版本:
<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.Extensions.Http" publicKeyToken="adb9793829ddae60" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.Extensions.DependencyInjection" publicKeyToken="adb9793829ddae60" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
内容的提问来源于stack exchange,提问作者Mikkel Rohde Glerup
相关产品推荐
相关产品推荐

