Powershell config assembly redirect

后端 未结 1 607
日久生厌
日久生厌 2020-12-14 03:11

I have a custom .NET assembly with some powershell cmdlets than I use for common domain related tasks. I\'ve just created a new cmdlet that references a 3rd party library th

1条回答
  •  囚心锁ツ
    2020-12-14 03:29

    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.

    0 讨论(0)
提交回复
热议问题