.NET Core 3.1 Azure Function中NHapi PipeParser运行抛异常如何解决
问题成因
- NHapi 3.0.4的
PackageManager类静态构造函数依赖System.Configuration.ConfigurationManager 5.0.0.0,该版本不属于.NET Core 3.1基础运行时自带的程序集,需要通过NuGet单独引入。 - .NET Core 3.1版本的Azure Functions进程内运行时存在特殊的程序集探测逻辑:运行时只会从应用根目录下的
bin子目录加载第三方依赖程序集,而NuGet默认会将System.Configuration.ConfigurationManager.dll输出到构建根目录(bin\Debug\netcoreapp3.1),不会自动同步到bin子目录,因此触发加载失败异常。
可选解决方案(无需Post Build复制逻辑)
方案1:强制所有NuGet依赖复制到输出子目录
在项目.csproj文件中添加CopyLocalLockFileAssemblies属性配置,同时显式引用对应版本的System.Configuration.ConfigurationManager,确保依赖被正确复制到bin子目录:
<PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <AzureFunctionsVersion>v3</AzureFunctionsVersion> <!-- 新增以下配置 --> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> </PropertyGroup> <ItemGroup> <!-- 显式声明依赖版本 --> <PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" /> <!-- 你的其他包引用 --> </ItemGroup>
方案2:添加程序集绑定重定向
在项目根目录新建app.config文件,添加绑定重定向规则,统一程序集版本加载逻辑:
<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Configuration.ConfigurationManager" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
方案3:升级运行时版本
如果业务允许,可将Azure Functions运行时升级到v4,目标框架调整为.NET 6及以上LTS版本,新版本运行时已修复程序集探测路径缺陷,不会出现此类依赖加载问题。
内容的提问来源于stack exchange,提问作者auburg
相关产品推荐
相关产品推荐

