Azure Functions从.NET 6升级到.NET 8遇System.Data.Common加载错误
Azure Functions v4从.NET 6升级到.NET 8后System.Data.Common加载失败问题解决
问题详情
将v4版本的Azure Functions应用从.NET 6升级到.NET 8,操作步骤为:
- 升级Visual Studio至17.9版本
- 修改项目目标框架为
net8.0 - 清理并构建项目后,运行时抛出错误:
System.Private.Corelib: 执行函数时发生异常:无法加载文件或程序集'System.Data.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken='。系统找不到指定的文件。
项目未直接引用System.Data.Common,显式安装该程序集也无法解决问题,项目属性已确认目标框架为.NET 8。
项目.csproj内容如下:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Azure.Identity" Version="1.6.0" /> <PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.3.0" /> <PackageReference Include="Microsoft.Azure.KeyVault" Version="3.0.5" /> <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" /> <PackageReference Include="System.Data.SqlClient" Version="4.8.3" /> </ItemGroup> <ItemGroup> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> </ItemGroup> </Project>
解决步骤
1. 升级兼容.NET 8的NuGet包
项目中多个包版本过低,存在.NET 8兼容性问题,需升级到对应最新稳定版:
- Microsoft.NET.Sdk.Functions:至少升级到4.2.0+(推荐最新稳定版,确保完全支持.NET 8)
- Azure.Identity:升级到1.10.x及以上版本
- Azure.Security.KeyVault.Secrets:升级到4.5.x及以上版本
- System.Data.SqlClient:升级到5.1.x及以上版本
2. 移除过时弃用的包
以下包已被新的Azure SDK取代,与.NET 8兼容性差,直接移除:
Microsoft.Azure.KeyVaultMicrosoft.Azure.Services.AppAuthentication
移除后,仅保留Azure.Identity和Azure.Security.KeyVault.Secrets用于Key Vault相关操作。
3. 添加程序集绑定重定向(可选,升级后仍报错时使用)
若升级包后仍出现加载错误,在项目根目录创建app.config文件,添加以下配置统一程序集版本:
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Data.Common" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
4. 彻底清理重建项目
- 执行Visual Studio的
清理解决方案操作 - 手动删除项目目录下的
bin和obj文件夹 - 右键项目选择
还原NuGet包 - 重新构建并运行项目
5. 确认Azure Functions Runtime版本
确保本地运行的Azure Functions Runtime为v4.25.0及以上版本(支持.NET 8的最低Runtime版本),可通过命令行更新:
func extensions install
内容的提问来源于stack exchange,提问作者sigma
相关产品推荐
相关产品推荐

