Powershell配置程序集重定向

use*_*754 15 .net powershell assembly-resolution

我有一个自定义.NET程序集与一些PowerShell cmdlet比我用于常见的域相关任务.我刚刚创建了一个新的cmdlet,它引用了第三方库,该库引用了Newtonsoft.Json 4.5.0.0.但是我的其他项目之一使用最新版本的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>
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用.融合日志确实表明它正在查找这个新的配置文件中的powershell,但它似乎没有拿起重定向.

有点难以解决这里的解决方案.任何线索可能是什么问题?同样的重定向在我的一些商业服务中起作用,否则会出现同样的问题(他们也使用第三方库和json.net 6).

干杯

dav*_*ola 24

不知道它是如何在一年多前工作的,但是今天在Windows 10上使用PowerShell 5.0.10240.16384我能够进行程序集重定向(在我的情况下从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)
Run Code Online (Sandbox Code Playgroud)

我首先FSharp.Core从某处加载正确版本的地方,因为GAC中的版本已经过时了(我想这可能也是你的情况)

您还可以检查我的项目中的实际测试用法.