azure - Azure 自动化帐户 PowerShell Runbook 中的绑定重定向
问题描述
在 Azure 自动化帐户运行手册中设置 PowerShell 绑定重定向时,我需要一些帮助。
本质上,我的 Runbook 调用了两个第 3 方 .Net dll 中的许多方法,它们都由同一作者提供。其中一个 dll 依赖于 Newtonsoft.Json V12.0.1,另一个依赖于 IdentityModel V4.0.0,而 IdentityModel V4.0.0 又依赖于 Newtonsoft.Json V11.0.2。我的 Azure 环境使用 Windows PowerShell Desktop V5.1.15063.726。在我的 Runbook 中做任何工作之前,我会调用一个函数来加载我导入的模块中的所有 dll(该函数为每个 dll 调用 [System.Reflection.Assembly]::LoadFrom() 方法)。我已验证我导入的 dll,包括 Newtonsoft.Json.dll V12.0.1 已成功加载。
正如预期的那样,当 Runbook 执行遇到调用需要 IdentityModel.dll 的第 3 方 dll 方法之一的行时,将引发异常:使用“0”参数调用“GetResult”的异常:“无法加载文件或程序集'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' 或其依赖项之一。系统找不到指定的文件。” System.IO.FileNotFoundException。
这是一个已知问题,到目前为止的共识似乎是我需要在 PowerShell 中创建和附加一个事件处理程序。可以在此处找到建议的解决方案之一。我的问题是尝试附加处理程序的行:
[System.AppDomain]::CurrentDomain.add_AssemblyResolve($onAssemblyResolveEventHandler)
当我的 Runbook 遇到此行时,将引发以下异常:找不到“add_AssemblyResolve”的重载和参数计数:“1”。TargetSite:无效 CheckActionPreference(System.Management.Automation.Language.FunctionContext,System.Exception) StackTrace:在 System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext,异常异常)
我可以确认,非常有限的测试似乎证明代码在使用 PowerShell ISE 的 Windows 8.1 中有效。我进行了广泛的搜索,以查看在 Azure 和 Windows 桌面中编写此代码的方式是否存在差异,但没有运气。谁能看到我做错了什么?我是否缺少任何 DLL 或使用 PowerShell 脚本中的语句?如果无法以这种方式重定向绑定,任何人都可以提供替代技术吗?
显示事件处理程序的创建和附加的代码摘录如下:
# Intercept resolution of binaries
$onAssemblyResolveEventHandler = [System.ResolveEventHandler]
{
param($sender, $e)
Write-Host "ResolveEventHandler: Attempting FullName resolution of $($e.Name)"
foreach($assembly in [System.AppDomain]::CurrentDomain.GetAssemblies())
{
if ($assembly.FullName -eq $e.Name)
{
Write-Host "Successful FullName resolution of $($e.Name)"
return $assembly
}
}
Write-Host "ResolveEventHandler: Attempting name-only resolution of $($e.Name)"
foreach($assembly in [System.AppDomain]::CurrentDomain.GetAssemblies())
{
# Get just the name from the FullName (no version)
$assemblyName = $assembly.FullName.Substring(0, $assembly.FullName.IndexOf(", "))
if ($e.Name.StartsWith($($assemblyName + ",")))
{
Write-Host "Successful name-only (no version) resolution of $assemblyName"
return $assembly
}
}
Write-Host "Unable to resolve $($e.Name)"
return $null
}
# Attach event handler
# This is the line that fails.
[System.AppDomain]::CurrentDomain.add_AssemblyResolve($onAssemblyResolveEventHandler)
解决方案
我无法解决在 Azure 自动化帐户 - PowerShell runbook 环境中工作的绑定重定向问题,但两个第 3 方 .Net dll 的作者已同意为两者使用 NewtonSoft.Json 版本 11.0.2 dll,所以我的问题消失了。
推荐阅读
- c# - Xamarin.Forms FlexLayout 调整为最大值
- javascript - JavaScript:检查所有数组是否包含相同的值?
- asp.net-core - asp-route-* 不在视图组件的视图内呈现查询字符串
- android - 谷歌地图自定义标记图标大小未在 Android 上保留
- powershell - 如何使用 Powershell 中的变量将一系列文件移动到文件夹中?
- python - 如何在 Keras 中正确连接 Dense 层和 Lambda 层?
- css - 2个宽度和超过三分之一宽度的媒体查询
- cocoa - NSNumberFormatter 通过 StoryBoard - Xcode 9
- java - 加速声纳扫描
- python - 更改 PySpark 中日期时间戳的显示格式