使用 Powershell 创建 signalR 客户端 HubConnection

Gab*_*ult 5 c# asp.net powershell nuget signalr

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

我已经设置了一个我在这里尝试过的最小的不起作用的示例 要点

相关代码:

加载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)
Run Code Online (Sandbox Code Playgroud)

创建集线器连接:

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

脚步:

  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

结果:

输出

在 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)
Run Code Online (Sandbox Code Playgroud)

这个 signalR 源代码对象似乎是问题所在,我只是不知道它的哪一部分会引发此错误。

问题:

当signalR 依赖项>=6.0.4,并且我有时,为什么错误提到 Newtonsoft.Json v6.0.0 10.0.2

我在 Powershell 脚本中是否做错了什么可能导致此问题?

非常感谢!此时如有任何帮助,我们将不胜感激

Gab*_*ult 5

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

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

这个要点的启发,这个想法是:

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

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

然后,设置重定向绑定。我最好的猜测是,这会拦截任何加载程序集的调用,使我们有机会返回自己的 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)

}
Run Code Online (Sandbox Code Playgroud)

最后,在脚本末尾,取消绑定

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