Gab*_*ult 5 c# asp.net powershell nuget signalr
我正在尝试使用 powershell 脚本连接到 SignalR 集线器。我对 powershell 很陌生,所以请原谅任何菜鸟错误。
我已经设置了一个我在这里尝试过的最小的不起作用的示例 :要点
$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)
.\HubConnectionTestsScript.ps1Error : 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 脚本中是否做错了什么可能导致此问题?
非常感谢!此时如有任何帮助,我们将不胜感激
在同事的帮助下,我设法解决了这个问题。在此分享解决方案,以防有人遇到同样的问题。
似乎 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)
| 归档时间: |
|
| 查看次数: |
2406 次 |
| 最近记录: |