从Signalr .net客户端调用集线器方法将引发异常

Invoking hub method from signalr .net client throws exception
2021-02-23
  •  译文(汉语)
  •  原文(英语)

我一直在尝试从Signalr .Net客户端调用集线器上的方法,但是它引发以下异常:

[ArgumentNullException: Value cannot be null.
Parameter name: s]
   System.IO.StringReader..ctor(String s) +10894409
   Microsoft.AspNet.SignalR.Json.JsonSerializerExtensions.Parse(JsonSerializer serializer, String json) +63
   Microsoft.AspNet.SignalR.Hubs.HubRequestParser.Parse(String data, JsonSerializer serializer) +19
   Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnReceived(IRequest request, String connectionId, String data) +40
   Microsoft.AspNet.SignalR.<>c__DisplayClassc.<ProcessRequest>b__7() +34
   Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod(Func`1 func) +28
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   Microsoft.AspNet.SignalR.Transports.<ProcessSendRequest>d__10.MoveNext() +364
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   Microsoft.Owin.Mapping.<Invoke>d__0.MoveNext() +386
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +69
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +64
   System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +415
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

这是当我尝试在远程主机上调用该方法时,在开发它时它确实在本地起作用的怪异事物.

这是集线器的代码:

public void UpdateLocalPlayerName(string name)
{
    ...

    foreach (var connection in websiteConnections)
    {
        Clients.Client(connection.ConnectionID).UpdateLocalPlayerName(Context.ConnectionId, name);
    }
}

这是我尝试使用.Net客户端调用它的方法

public void UpdateLocalPlayerName(string name)
{
    MainHubProxy.Invoke<string>("UpdateLocalPlayerName", name);
}

我正在使用2.0.3.0版本的.Net客户端,并且服务器运行IIS8 ...

知道是什么原因造成的吗?

速聊1:
您在客户端传递的变量"名称"可能为Null
速聊2:
可以肯定它不是,因为它在本地工作.
速聊3:
这不是您的情况,但我遇到了同样的问题,在我的情况下,异常是由使用HTTP连接时代理服务器中的重定向引起的.切换到HTTPS,它就像一个魅力!
解决过程1

好像是我有www.我连接到的服务器url的前面,猜测路由阻止了参数正确传递.将该网址更改为不带www的网址,并且可以正常工作.

I've been trying to call a method on my hub from a signalr .Net client however it throws the following exception:

[ArgumentNullException: Value cannot be null.
Parameter name: s]
   System.IO.StringReader..ctor(String s) +10894409
   Microsoft.AspNet.SignalR.Json.JsonSerializerExtensions.Parse(JsonSerializer serializer, String json) +63
   Microsoft.AspNet.SignalR.Hubs.HubRequestParser.Parse(String data, JsonSerializer serializer) +19
   Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnReceived(IRequest request, String connectionId, String data) +40
   Microsoft.AspNet.SignalR.<>c__DisplayClassc.<ProcessRequest>b__7() +34
   Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod(Func`1 func) +28
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   Microsoft.AspNet.SignalR.Transports.<ProcessSendRequest>d__10.MoveNext() +364
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   Microsoft.Owin.Mapping.<Invoke>d__0.MoveNext() +386
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +69
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +64
   System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +415
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

This is when I try to invoke the method on a remote host, the weird thing when developing it it actually does work locally...

Here is the code of the hub:

public void UpdateLocalPlayerName(string name)
{
    ...

    foreach (var connection in websiteConnections)
    {
        Clients.Client(connection.ConnectionID).UpdateLocalPlayerName(Context.ConnectionId, name);
    }
}

And here is how I try to invoke it with the .Net client

public void UpdateLocalPlayerName(string name)
{
    MainHubProxy.Invoke<string>("UpdateLocalPlayerName", name);
}

I am using 2.0.3.0 version of the .Net client and the server runs IIS8...

Any idea what causes this?

Talk1:
Its possible the variable "name" you pass on the client side is Null
Talk2:
Pretty sure it isn't, as it works locally.
Talk3:
It's not your case, but I was facing same problem and in my case, the exception was caused by a redirection in the proxy server when connecting using HTTP. Switched to HTTPS and it worked like a charm!
Solutions1

Seems to be I had www. infront of the server's url I was connecting to, guess the routing prevented the arguments from being passed correctly. Changed the url to the url without www and it worked.

转载于:https://stackoverflow.com/questions/23712154/invoking-hub-method-from-signalr-net-client-throws-exception

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入