升级.NET Framework至4.6.2后Azure Functions部署报错求助
原本基于.NET Framework 4.6.1的控制台应用部署到Azure Functions后运行正常,因更新NuGet包需将目标框架升级至4.6.2,本地调试无异常,但部署到Azure后函数无法正常运行,报错信息如下:
2022-09-08T13:07:22.403 [Error] D:\home\site\wwwroot\bin\CreatePlanningAndUserOverviewYearly.exe : System.Management.Automation.RemoteExceptionat run.ps1: line 1+ D:\home\site\wwwroot\bin\CreatePlanningAndUserOverviewYearly.exe+ ________________________________________________________________+ CategoryInfo : NotSpecified: (:String) [], RemoteException+ FullyQualifiedErrorId : NativeCommandError
2022-09-08T13:07:22.416 [Error] D:\home\site\wwwroot\bin\CreatePlanningAndUserOverviewYearly.exe : Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Xrm.Tooling.Connector, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)at UES.CRM.Backend.CreatePlanningAndUserOverviewYearly.Main(String[] args)at run.ps1: line 1+ D:\home\site\wwwroot\bin\CreatePlanningAndUserOverviewYearly.exe+ ________________________________________________________________+ CategoryInfo : NotSpecified: (:) [], RemoteException+ FullyQualifiedErrorId : NativeCommandErrorMessage
2022-09-08T13:07:22.463 [Error] Exception while executing function: Functions.CreatePlanningAndUserOverviewYearly. Microsoft.Azure.WebJobs.Script: PowerShell script error. .
2022-09-08T13:07:22.478 [Error] Function completed (Failure, Id=fb0c79e2-d4bc-4992-b1ec-292f8b77db62, Duration=1494ms)
Azure端操作步骤
- 配置函数应用的.NET Framework版本:进入Azure Functions门户,找到对应函数应用的「配置」>「常规设置」,将「.NET Framework版本」设置为v4.6.2或更高(如v4.7.2),保存后重启函数应用。
- 清理并重新部署完整包:通过Kudu站点或Azure门户的高级工具,删除
wwwroot/bin目录下的所有旧文件,重新上传包含所有依赖项的最新发布包(确保本地发布时勾选「复制本地所有依赖」,避免缺失或版本不匹配的程序集)。 - 添加程序集绑定重定向配置:在函数应用的
wwwroot目录下创建或修改web.config文件,添加针对Microsoft.Xrm.Tooling.Connector的绑定重定向规则,示例如下:<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.Xrm.Tooling.Connector" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="实际部署的版本号" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> - 验证依赖版本一致性:通过Kudu站点的「Debug console」>「CMD」查看
bin目录下的Microsoft.Xrm.Tooling.Connector.dll版本,确认其与程序集引用的版本一致,若不一致则替换为正确版本的DLL文件。 - 检查PowerShell运行栈版本:确认函数应用的运行栈为PowerShell,且版本与本地调试环境兼容,必要时在「配置」>「常规设置」中升级PowerShell运行栈版本。
内容的提问来源于stack exchange,提问作者Furkan Karacan

