[英]SignalR server --> client call not working
我目前正在使用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<EventHub>();
container.Register<ManagementHub>();
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>();
managementHub.Clients.All.closeRequested();
我從未在CloseRequestedCallback
收到任何回調。 無論是在客戶端還是在服務器端,我都沒有在日志中出現任何錯誤。
我在這做錯了什么?
編輯09/10/15
經過一些研究和修改后,我發現它與更換IoC容器有關。 當我刪除與LightInject鏈接的所有內容並按原樣使用SignalR時,一切正常。 我對此感到驚訝,因為LightInject 記錄了它們與SignalR 的集成 。
我發現在這之后,我意識到GlobalHost.DependencyResolver
是不一樣的一個,我提供給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容器+依賴注入
如果你想改變國際奧委會你HubConfiguration
,你也需要改變從一個GlobalHost
所以要求它ouside上下文時返回相同的輪轂。
問題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;
}
現在一切都運行正常,我沒有遇到任何僵局。
有關問題和答案的更多詳細信息,您還可以參考我的問題中的編輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.