Powershell 配置程序集重定向
我有一個帶有一些 powershell cmdlet 的自定義 .NET 程序集,而不是用於常見域相關任務。我剛剛創建了一個新的 cmdlet,它引用了一個引用了 Newtonsoft.Json 4.5.0.0 的 3rd 方庫。然而,我的其他項目之一使用最新版本的 json.net (6.0.0.0)。所以在powershell fusion執行時會拋出一個錯誤,說它無法載入newtonsoft.json 4.5.0.0。
我嘗試創建一個 powershell.exe.config 並在其中放置一個程序集重定向:
<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json", Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed/> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>但這似乎不起作用。融合日誌確實表明它正在為 powershell 尋找這個新的配置文件,但它似乎沒有拾取重定向。
有點難為這裡的解決方案。任何線索可能是什麼問題?同樣的重定向適用於我的一些業務服務,否則它們會遇到相同的問題(他們也使用這個第 3 方庫和 json.net 6)。
乾杯
不知道一年多前它是如何工作的,但是今天在使用 PowerShell 5.0.10240.16384 的 Windows 10 上,我能夠進行程序集重定向(在我的情況下是從
FSharp.Core4.3 到 4.4)的唯一方法是基於手動解析程序集手動解決程序集依賴項PowerShell 中的依賴項。我嘗試了所有其他解決方案,例如創建powershell.exe.config文件或嘗試載入其他解決方案*.config file,但這些都沒有奏效。唯一的“陷阱”(至少對我來說)是,因為我在任何地方都沒有FSharp.Core 4.3,所以我需要手動將其重定向到 4.4。我最終使用
$FSharpCore = [reflection.assembly]::LoadFrom($PSScriptRoot + "\bin\LIBRARY\FSharp.Core.dll") $OnAssemblyResolve = [System.ResolveEventHandler] { param($sender, $e) # from:FSharp.Core, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a # to: FSharp.Core, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a if ($e.Name -eq "FSharp.Core, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") { return $FSharpCore } foreach($a in [System.AppDomain]::CurrentDomain.GetAssemblies()) { if ($a.FullName -eq $e.Name) { return $a } } return $null } [System.AppDomain]::CurrentDomain.add_AssemblyResolve($OnAssemblyResolve)我首先
FSharp.Core從某個地方載入正確的版本,因為 GAC 中的版本很舊(我想這也可能是你的情況)您還可以在我的項目中查看真實的測試使用情況。