SignalR服务器 – >客户端呼叫无法正常工作

我目前正在使用SignalR在服务器和服务器本身产生的多个独立进程之间进行通信。 Server和Client都用C#编码。 我正在使用SignalR 2.2.0.0在服务器端,我使用OWIN来运行服务器。 我也使用LightInject作为IoC容器。

这是我的代码:

public class AgentManagementStartup { public void ConfigurationOwin(IAppBuilder app, IAgentManagerDataStore dataStore) { var serializer = new JsonSerializer { PreserveReferencesHandling = PreserveReferencesHandling.Objects, TypeNameHandling = TypeNameHandling.Auto, TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple }; var container = new ServiceContainer(); container.RegisterInstance(dataStore); container.RegisterInstance(serializer); container.Register(); container.Register(); var config = container.EnableSignalR(); app.MapSignalR("", config); } } 

在客户端,我这样注册:

 public async Task Connect() { try { m_hubConnection = new HubConnection(m_serverUrl, false); m_hubConnection.Closed += OnConnectionClosed; m_hubConnection.TraceLevel = TraceLevels.All; m_hubConnection.TraceWriter = Console.Out; var serializer = m_hubConnection.JsonSerializer; serializer.TypeNameHandling = TypeNameHandling.Auto; serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects; m_managementHubProxy = m_hubConnection.CreateHubProxy(AgentConstants.ManagementHub.Name); m_managementHubProxy.On("closeRequested", CloseRequestedCallback); await m_hubConnection.Start(); } catch (Exception e) { m_logger.Error("Exception encountered in Connect method", e); } } 

在服务器端,我通过以下方式发送关闭请求:

 var managementHub = GlobalHost.ConnectionManager.GetHubContext(); managementHub.Clients.All.closeRequested(); 

我从未在CloseRequestedCallback收到任何回调。 无论是在客户端还是在服务器端,我都没有在日志中出现任何错误。

我在这做错了什么?

编辑09/10/15

经过一些研究和修改后,我发现它与更换IoC容器有关。 当我删除与LightInject链接的所有内容并按原样使用SignalR时,一切正常。 我对此感到惊讶,因为LightInject 记录了它们与SignalR 的集成 。

在我发现这个之后,我意识到HubConfiguration与我提供给HubConfiguration 。 一旦我加入

 GlobalHost.DependencyResolver = config.Resolver; 

之前

 app.MapSignalR("", config); 

我现在在CloseRequestedCallback接收回调。 不幸的是,一旦我从客户端调用从服务器到服务器的方法,我就会收到以下错误:

Microsoft.AspNet.SignalR.Client.Infrastructure.SlowCallbackException

检测到可能的死锁。 使用“HubProxy.On”或“Connection.Received”注册的回调已执行至少10秒。

我不确定我找到的修复程序以及它可能对系统产生的影响。 是否可以将GlobalHost.DependencyResolver替换为我自己的而不注册其所有默认内容?

编辑2 09/10/15

据此,更改GlobalHost.DependencyResolver是正确的做法。 仍然没有解释SlowCallbackException因为我在所有的回调中都没有做任何事情。

问题1:IoC容器+dependency injection

如果要为HubConfiguration更改IoC,还需要更改HubConfiguration的IoC,以便在请求上下文时返回相同的集线器。

问题2:意外的SlowCallbackException

这个例外是由我在控制台应用程序中使用SignalR引起的。 应用程序的入口点不能是async方法,因此我可以异步调用我的初始配置,如下所示:

 private static int Main() { var t = InitAsync(); t.Wait(); return t.Result; } 

对我来说不幸的是,这会导致很多问题,如此处所述以及更多细节。

通过启动我的InitAsync如下:

 private static int Main() { Task.Factory.StartNew(async ()=> await InitAsync()); m_waitInitCompletedRequest.WaitOne(TimeSpan.FromSeconds(30)); return (int)EndpointErrorCode.Ended; } 

现在一切都运行正常,我没有遇到任何僵局。

有关问题和答案的更多详细信息,您也可以参考我的问题中的编辑。