在问一个单独的问题之前,我已经做了很多关于它的谷歌搜索,并在已经存在的 stackoverflow问题中添加了一条评论。
我的服务器中有一个 SignalR Hub(尝试了 v. 1.1.3 和 2.0.0-rc),代码如下:
[HubName("TestHub")]
public class TestHub : Hub
{
[Authorize]
public void TestMethod(string test)
{
//some stuff here
Clients.Caller.NotifyOnTestCompleted();
}
}
如果我删除 Authorize 属性,问题仍然存在。
在我的 iOS 客户端中,我尝试使用以下代码调用它:
SRHubConnection *hubConnection = [SRHubConnection connectionWithURL:_baseURL];
SRHubProxy *hubProxy = [hubConnection createHubProxy:@"TestHub"];
[hubProxy on:@"NotifyOnTestCompleted" perform:self selector:@selector(stopConnection)];
hubConnection.started = ^{
[hubProxy invoke:@"TestMethod" withArgs:@[@"test"]];
};
//received, error handling
[hubConnection start];
当应用程序启动时,用户未登录并且没有打开的 SignalR 连接。用户通过调用服务器中的登录服务来登录,该服务使用WebSecurity.Login
方法。如果登录服务返回成功,然后我对 SignalR Hub 进行上述调用,我得到服务器错误 500,描述为“ConnectionId 的格式不正确。”。
完整的服务器堆栈跟踪如下:
Exception information:
Exception type: InvalidOperationException
Exception message: The ConnectionId is in the incorrect format.
at Microsoft.AspNet.SignalR.PersistentConnection.GetConnectionId(HostContext context, String connectionToken)
at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest(HostContext context)
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ProcessRequest(HostContext context)
at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest(IDictionary`2 environment)
at Microsoft.Owin.Mapping.MapMiddleware.<Invoke>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar)
at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Request information:
Request URL: http://myserverip/signalr/signalr/connect?transport=webSockets&connectionToken=axJs EQMZxpmUopL36owSUkdhNs85E0fyB2XvV5R5znZfXYI/CiPbTRQ3kASc3 mq60cLkZU7coYo1P fbC0U1LR2rI6WIvCNIMOmv/mHut/Unt9mX3XFkQb053DmWgCan5zHA==&connectionData=[{"Name":"testhub"}]
Request path: /signalr/signalr/connect
User host address:
User:
Is authenticated: False
Authentication Type:
Thread account name: IIS APPPOOL\DefaultAppPool
Thread information:
Thread ID: 14
Thread account name: IIS APPPOOL\DefaultAppPool
Is impersonating: True
Stack trace: at Microsoft.AspNet.SignalR.PersistentConnection.GetConnectionId(HostContext context, String connectionToken)
at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest(HostContext context)
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ProcessRequest(HostContext context)
at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest(IDictionary`2 environment)
at Microsoft.Owin.Mapping.MapMiddleware.<Invoke>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar)
at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
我知道这是某种身份验证和用户身份不匹配,但到目前为止我还没有找到解决它的方法。所有其他问题都建议在用户身份更改时停止打开的连接,但正如我上面提到的,在用户成功登录之前我没有打开的连接。
任何帮助将非常感激。谢谢你。