PowerShell的配置组装重定向 [英] Powershell config assembly redirect

查看:199
本文介绍了PowerShell的配置组装重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的.NET程序集与一些比​​我使用的公共域相关任务的PowerShell命令。我刚刚创建了一个新的cmdlet,引用引用了Newtonsoft.Json 4.5.0.0第三方库。但是我的其他项目之一使用json.net(6.0.0.0)的最新版本。因此,在运行PowerShell中融合抛出一个错误说,它不能加载newtonsoft.json 4.5.0.0。

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 that has a reference to Newtonsoft.Json 4.5.0.0. However one of my other projects uses the latest version of json.net (6.0.0.0). So at runtime in powershell fusion throws an error saying it can't load newtonsoft.json 4.5.0.0.

我试图创造一个powershell.exe.config并把装配重定向有:

I've tried creating a powershell.exe.config and putting an assembly redirect in there:

<?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的这个新的配置文件,但它似乎并没有被拿起重定向。

but this doesn't seem to work. The fusion log does state that it is looking in this new config file for powershell but it doesn't seem to be picking up the redirect.

位难倒解决方案在这里。任何线索是什么问题呢?同样的重定向工作在我的一些商业服务,否则将有同样的问题(他们也用这个第三方lib和json.net 6)。

Bit stumped for solutions here. Any clues what the problem might be? This same redirect works in some of my business services that would otherwise have the same issue (they also use this 3rd party lib and json.net 6).

干杯

推荐答案

不知道它是如何然而今天使用PowerShell的5.0.10240.16384我能够做组装重定向的唯一方法(在工作一年多以前,在Windows 10我情况下,从 FSharp.Core 4.3〜4.4)是基于的手动解决集依赖在PowerShell中。我试过所有其他解决方案,如创建 powershell.exe.config 文件或尝试加载一些其他的 *。配置文件 ,但没有这些的工作。

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.

唯一的疑难杂症(在租赁对我来说)是,既然我做的不可以有FSharp.Core 4.3的任何地方,我需要手动将其重定向到4.4。最后我用

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)

在这里,我第一次从某处装载了正确版本的 FSharp.Core 在GAC中的版本是旧的(我想这可能是你的情况也是如此)

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.

这篇关于PowerShell的配置组装重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆