Powershell config assembly redirect

落爺英雄遲暮 提交于 2019-11-28 06:57:18

Not sure how it worked more than year ago, however today on Windows 10 using PowerShell 5.0.10240.16384 the only way I was able to do assembly redirect (in my case from FSharp.Core 4.3 to 4.4) was to manually resolve assembly dependencies based on Manually resolving assembly dependencies in PowerShell. I tried every other solutions like creating the powershell.exe.config file or trying to load some other *.config file, but nothing of those worked.

The only "gotcha" (at lease for me) was, that since I do not have FSharp.Core 4.3 anywhere, I needed to manually redirect it to 4.4. I ended up using

$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)

where I am first loading the correct version of FSharp.Core from somewhere as the version in the GAC is old (I guess this might be your case too)

You can also check the real test usage in my project.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!