Azure Logic App本地函数添加依赖后程序集版本冲突求解
问题:Azure Logic App Standard本地函数程序集版本冲突(.NET Framework 4.7.2)
基于.NET Framework 4.7.2创建的Azure Logic App Standard本地函数,原本运行正常,添加Azure.Identity、Azure.Messaging.ServiceBus等外部依赖后,出现程序集加载失败的版本冲突问题。
项目csproj配置:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <IsPackable>false</IsPackable> <TargetFramework>net472</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> <OutputType>Library</OutputType> <PlatformTarget>x64</PlatformTarget> <!-- Please replace 'LogicAppFolder' with the name of your folder that contains your logic app project. --> <LogicAppFolder>LogicApp</LogicAppFolder> <CopyToOutputDirectory>Always</CopyToOutputDirectory> <LangVersion>latest</LangVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Azure.Identity" Version="1.12.0" /> <PackageReference Include="Azure.Messaging.ServiceBus" Version="7.17.5" /> <PackageReference Include="Microsoft.Azure.WebJobs.Core" Version="3.0.41" /> <PackageReference Include="Microsoft.Azure.Workflows.WebJobs.Sdk" Version="1.1.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.4.1" /> </ItemGroup> <Target Name="Task" AfterTargets="Compile"> <ItemGroup> <DirsToClean2 Include="..\$(LogicAppFolder)\lib\custom" /> </ItemGroup> <RemoveDir Directories="@(DirsToClean2)" /> </Target> <Target Name="CopyExtensionFiles" AfterTargets="ParameterizedFunctionJsonGenerator"> <ItemGroup> <CopyFiles Include="$(MSBuildProjectDirectory)\bin\$(Configuration)\net472\**\*.*" CopyToOutputDirectory="PreserveNewest" Exclude="$(MSBuildProjectDirectory)\bin\$(Configuration)\net472\*.*" /> <CopyFiles2 Include="$(MSBuildProjectDirectory)\bin\$(Configuration)\net472\*.*" /> </ItemGroup> <Copy SourceFiles="@(CopyFiles)" DestinationFolder="..\$(LogicAppFolder)\lib\custom\%(RecursiveDir)" SkipUnchangedFiles="true" /> <Copy SourceFiles="@(CopyFiles2)" DestinationFolder="..\$(LogicAppFolder)\lib\custom\net472\" SkipUnchangedFiles="true" /> <ItemGroup> <MoveFiles Include="..\$(LogicAppFolder)\lib\custom\bin\*.*" /> </ItemGroup> <Move SourceFiles="@(MoveFiles)" DestinationFolder="..\$(LogicAppFolder)\lib\custom\net472" /> <ItemGroup> <DirsToClean Include="..\$(LogicAppFolder)\lib\custom\bin" /> </ItemGroup> <RemoveDir Directories="@(DirsToClean)" /> </Target> <ItemGroup> <Reference Include="Microsoft.CSharp" /> </ItemGroup> <ItemGroup> <Folder Include="bin\$(Configuration)\net472\" /> </ItemGroup> </Project>
运行时异常:
Result: Function 'ReadQueueSessionMessage', Invocation id '6eda05ec-d18d-41bf-832b-c02144b18cf7': An exception was thrown by the invocation.
Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Azure.Core, Version=1.38.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8' or one of its dependencies. The system cannot find the file specified.
解决方案
1. 启用绑定重定向(支持Logic App本地函数)
基于.NET Framework的Logic App本地函数支持绑定重定向,操作步骤:
- 在函数项目中添加
app.config文件,配置Azure.Core等程序集的绑定重定向规则,示例:<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Azure.Core" publicKeyToken="92742159e12e44c8" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.38.0.0" newVersion="1.38.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> - 修改csproj,确保
app.config被复制到输出目录:<ItemGroup> <None Include="app.config"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> </ItemGroup> - 更新
CopyExtensionFiles目标,将app.config复制到Logic App的lib/custom/net472目录下。
2. 对齐依赖包版本
- 检查Azure.Identity 1.12.0与Azure.Messaging.ServiceBus 7.17.5依赖的Azure.Core版本,手动添加Azure.Core的PackageReference,指定两者兼容的版本:
<PackageReference Include="Azure.Core" Version="1.38.0" /> - 升级或降级相关Azure SDK包到互相兼容的版本,避免版本不一致导致的依赖缺失。
3. 修复程序集复制逻辑
当前的CopyExtensionFiles目标可能遗漏部分依赖程序集:
- 修改
CopyFiles的包含规则,确保所有子目录下的依赖dll都被复制;或使用MSBuild的ResolveAssemblyReferences任务获取所有依赖项后再复制,保证Azure.Core等dll被正确复制到lib/custom/net472目录。
4. 解决Azure.Identity已知问题
针对Azure.Identity的相关bug,直接升级到最新稳定版本,或降级到无该问题的版本,同时确保新版本与其他依赖兼容。
内容的提问来源于stack exchange,提问作者Rob Bowman
相关产品推荐
相关产品推荐

