.NET 6升级.NET 8 Azure函数报错:无法加载Microsoft.Extensions.Options
问题分析与解决方案
你的Azure函数应用升级到.NET 8时出现Microsoft.Extensions.Options, Version=8.0.0.0加载失败,核心原因是项目同时混合了Azure Functions In-Process模型和Isolated Worker模型的依赖包,两种模型的依赖体系不兼容,导致版本冲突和程序集加载异常。
结合你的csproj文件,具体冲突点包括:
- 同时引用了In-Process模型核心包
Microsoft.NET.Sdk.Functions和Isolated模型核心包Microsoft.Azure.Functions.Worker - 混合使用了In-Process的Durable包
Microsoft.Azure.WebJobs.Extensions.DurableTask和Isolated的Durable包Microsoft.Azure.Functions.Worker.Extensions.DurableTask Microsoft.Extensions.Options系列包版本不一致(8.0.2和8.0.0混用)
具体解决步骤
1. 清理冲突依赖包
移除所有In-Process模型专属的包,保留Isolated Worker模型的核心依赖:
- 删除
Microsoft.NET.Sdk.Functions、Microsoft.Azure.WebJobs.Extensions.DurableTask、Microsoft.Azure.WebJobs.Extensions.OpenApi.Core - 保留
Microsoft.Azure.Functions.Worker、Microsoft.Azure.Functions.Worker.Extensions.Http、Microsoft.Azure.Functions.Worker.Extensions.DurableTask等Isolated模型包
2. 统一Microsoft.Extensions.Options系列包版本
将所有Microsoft.Extensions.Options相关包(包括ConfigurationExtensions、DataAnnotations)统一为相同版本(建议使用你已安装的8.0.2),避免跨包版本冲突。
3. 同步类库项目的.NET版本与依赖
确保你引用的Project.Contract和Project.Data两个类库项目:
- 已升级到.NET 8
- 它们的
Microsoft.Extensions.Options相关依赖版本与主函数项目一致
4. 清理构建缓存并重建
- 删除项目根目录下的
bin和obj文件夹 - 在Visual Studio中执行清理解决方案 → 重建解决方案
- 或使用命令行:
dotnet clean dotnet restore dotnet build
5. 验证Isolated模型配置
确认local.settings.json中FUNCTIONS_WORKER_RUNTIME值为dotnet-isolated,无其他冲突配置。
修正后的核心csproj示例
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> <NoWarn>1591</NoWarn> <Nullable>enable</Nullable> <OutputType>Exe</OutputType> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> <ItemGroup> <PackageReference Include="Azure.Identity" Version="1.13.1" /> <PackageReference Include="Azure.Storage.Blobs" Version="12.22.2" /> <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" /> <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" /> <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.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.3.2" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.1.7" /> <PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="4.0.1" /> <!-- 统一Options系列包版本 --> <PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" /> <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.2" /> <PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.2" /> <PackageReference Include="System.Formats.Asn1" Version="8.0.1" /> <PackageReference Include="System.Net.Http" Version="4.3.4" /> <PackageReference Include="System.Security.Cryptography.Pkcs" Version="8.0.1" /> <PackageReference Include="System.ServiceModel.Duplex" Version="6.0.0" /> <PackageReference Include="System.ServiceModel.Http" Version="8.0.0" /> <PackageReference Include="System.ServiceModel.NetTcp" Version="8.0.0" /> <PackageReference Include="System.ServiceModel.Security" Version="6.0.0" /> <PackageReference Include="System.Text.Json" Version="8.0.5" /> <PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Project.Contract\Project.Contract.csproj" /> <ProjectReference Include="..\Project.Data\Project.Data.csproj" /> </ItemGroup> <ItemGroup> <None Update="Data\RecourceType.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> </ItemGroup> <ItemGroup> <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" /> </ItemGroup> </Project>
补充说明
如果需要OpenApi支持,In-Process模型的Microsoft.Azure.WebJobs.Extensions.OpenApi.Core不适用于Isolated Worker,需改用兼容Isolated模型的包(比如社区维护的AlohaKit.AzureFunctions.Extensions.OpenApi)。
内容的提问来源于stack exchange,提问作者Saurav
相关产品推荐
相关产品推荐

