.NET 8升级后Microsoft Azure Function App运行异常求助
我已将项目升级至.NET 8,本地运行一切正常,但部署到Azure Function App后出现以下错误:
System.Private.CoreLib: 无法从程序集'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=xyz123'中加载类型'System.Runtime.CompilerServices.NullableContextAttribute'。
目前除了Microsoft.IdentityModel.Protocols.OpenIdConnect(升级到7.1.2+版本后本地运行会崩溃,暂未升级),其余NuGet包均已更新至最新版本。
启动项目的.csproj配置如下:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> <TargetFramework>net8.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.37" /> <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.16.4" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.4.1" /> <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" /> <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" /> <PackageReference Include="PdfPig" Version="0.1.8" /> </ItemGroup> <ItemGroup> <None Update="appsettings.Development.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="appsettings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="blmgencmd.exe"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> <None Update="msvcr110d.dll"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> </ItemGroup> <ItemGroup> <ProjectReference Include="..\xyz.csproj" /> //here are some more </ItemGroup> </Project>
访问Function App时返回:
503 Service unavailable - Function host is not running.
已确认Function App配置已设置为.NET 8,求分析问题原因或排查方向。
排查方向与解决方案
修复程序集绑定重定向
自动生成的绑定重定向在Azure环境可能未生效,手动添加app.config文件指定重定向规则:<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>发布后检查输出目录下的
{YourFunctionName}.dll.config是否包含该规则。统一Azure Functions相关包版本
当前使用的Microsoft.Azure.WebJobs(3.0.37)、Microsoft.NET.Sdk.Functions(4.4.1)版本偏旧,存在.NET 6依赖残留风险:- 升级
Microsoft.NET.Sdk.Functions至适配.NET 8的最新稳定版 - 升级
Microsoft.Azure.WebJobs到3.2.0+版本 - 同步更新
Microsoft.Azure.WebJobs.Extensions.ServiceBus至对应兼容版本
- 升级
排查IdentityModel包的本地崩溃问题
暂未升级的Microsoft.IdentityModel.Protocols.OpenIdConnect可能是版本冲突根源:- 执行
dotnet list package --outdated查看依赖链,定位本地崩溃的具体原因 - 尝试升级到7.1.2+版本后,通过启用详细日志(
dotnet run -v detailed)定位崩溃点,解决后完成升级,消除.NET 6依赖
- 执行
验证Azure运行时配置
- 确认Function App的运行时堆栈设置为
.NET 8(区分In-Process/Isolated模式,需与项目类型匹配) - 检查应用设置中
FUNCTIONS_EXTENSION_VERSION是否为~4,与项目中AzureFunctionsVersion保持一致
- 确认Function App的运行时堆栈设置为
清理发布残留文件
- 本地执行
dotnet clean后再发布,避免旧文件干扰 - Azure端发布时勾选
Delete existing files选项,清除部署目录中的.NET 6程序集残留
- 本地执行
检查依赖项目的.NET版本
所有ProjectReference指向的项目必须同步升级至.NET 8,跨版本依赖会导致程序集加载冲突。
内容的提问来源于stack exchange,提问作者jensDR

