【问题标题】:Creating a signalR client HubConnection with Powershell使用 Powershell 创建 signalR 客户端 HubConnection
【发布时间】:2017-11-18 06:20:35
【问题描述】:

我正在尝试使用 powershell 脚本连接到 SignalR 集线器。我对powershell很陌生,所以请原谅任何菜鸟的错误。

我已经设置了一个最小的 not 我在这里尝试过的工作示例: Gist

相关代码:

加载 dll

$dllFolder = -join((Get-Item -Path ".\" -Verbose).FullName, "\bin\Debug\")
[string[]] $dllPathsToLoad = @("\Newtonsoft.Json.dll", "\Microsoft.AspNet.SignalR.Client.dll")
$token = "insertyourtokenhere"

function LoadDllPaths($dlls)
{
    foreach ($dll in $dlls)
    {
        $dllpath = $dllFolder + $dll
        [System.Reflection.Assembly]::LoadFrom($dllpath)
    }
}
[...]
LoadDllPaths($dllPathsToLoad)

创建 HubConnection:

$server = "https://localhost/rest/"
[...]
$hub = New-Object Microsoft.AspNet.SignalR.Client.HubConnection($server)

步骤:

  1. 创建一个新的 Visual Studio 项目
  2. 添加 Newtonsoft.Json v10.0.2 Nuget 包(最新)
  3. 添加 Microsoft.AspNet.SignalR.Client v2.2.2 Nuget 包(最新)
  4. 将 powershell 脚本添加到项目的根目录
  5. 使用 powershell(以管理员身份运行),键入 .\HubConnectionTestsScript.ps1

结果:

View on imgur

Error : System.Management.Automation.MethodInvocationException: Exception calling ".ctor" with "1" argument(s): "Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified." ---> System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
   at Microsoft.AspNet.SignalR.Client.Connection..ctor(String url, String queryString)
   at Microsoft.AspNet.SignalR.Client.HubConnection..ctor(String url, Boolean useDefaultUrl)
   --- End of inner exception stack trace ---
   at System.Management.Automation.DotNetAdapter.AuxiliaryConstructorInvoke(MethodInformation methodInformation, Object[] arguments, Object[] originalArguments)
   at System.Management.Automation.DotNetAdapter.ConstructorInvokeDotNet(Type type, ConstructorInfo[] constructors, Object[] arguments)
   at Microsoft.PowerShell.Commands.NewObjectCommand.CallConstructor(Type type, ConstructorInfo[] constructors, Object[] args)

This signalR source code object 似乎是问题所在,我只是看不出它的哪一部分会引发此错误。

问题:

signalR dependencies>=6.0.4,而我有10.0.2 时,为什么错误会提到Newtonsoft.Json v6.0.0?

我是否在我的 Powershell 脚本中做错了可能导致此问题的任何事情?

非常感谢!任何帮助在这一点上表示赞赏

【问题讨论】:

    标签: c# asp.net powershell nuget signalr


    【解决方案1】:

    在一位同事的帮助下,我设法解决了这个问题。在这里分享解决方案,以防有人遇到同样的问题。

    似乎有一个 SignalR 依赖项试图加载旧版本的 Newtonsoft.Json。我们可以强制它把他重定向到我们自己的 Newtonsoft.Json 实例

    this gist 启发,想法如下:

    加载 Json 程序集时,将其存储在变量中

    $newtonsoftAssembly = [System.Reflection.Assembly]::LoadFrom($dllFolder + "\Newtonsoft.Json.dll")
    

    然后,设置重定向绑定。我最好的猜测是,这会拦截任何加载程序集的调用,让我们有机会返回我们自己的 Json 程序集,而不是让他找不到他想要的版本(在我的例子中是 6.0.0)。

    function RedirectJsonBindings()
    {
        $onAssemblyResolveEventHandler = [System.ResolveEventHandler] {
            param($sender, $e)
            # You can make this condition more or less version specific as suits your requirements
            if ($e.Name.StartsWith("Newtonsoft.Json")) {
                Write-Host "Newtonsoft assembly" $e.Name -ForegroundColor DarkGreen
                return $newtonsoftAssembly
            }
            foreach($assembly in [System.AppDomain]::CurrentDomain.GetAssemblies()) {
                if ($assembly.FullName -eq $e.Name) {
                    return $assembly
                }
            }
            return $null
        }
        [System.AppDomain]::CurrentDomain.add_AssemblyResolve($onAssemblyResolveEventHandler)
    
    }
    

    最后,在脚本的最后,解除绑定

    # Detach the event handler (not detaching can lead to stack overflow issues when closing PS)
    [System.AppDomain]::CurrentDomain.remove_AssemblyResolve($onAssemblyResolveEventHandler)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多