KOT*_*TIX 6 c# signalr owin light-inject
我目前正在使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
在客户端,我这样注册:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
在服务器端,我通过以下方式发送关闭请求:
var managementHub = GlobalHost.ConnectionManager.GetHubContext<ManagementHub>();
managementHub.Clients.All.closeRequested();
Run Code Online (Sandbox Code Playgroud)
我从来没有接到任何回调CloseRequestedCallback.无论是在客户端还是在服务器端,我都没有在日志中出现任何错误.
我在这做错了什么?
编辑09/10/15
经过一些研究和修改后,我发现它与更换IoC容器有关.当我删除与LightInject链接的所有内容并按原样使用SignalR时,一切正常.我对此感到惊讶,因为LightInject 记录了它们与SignalR 的集成.
在我发现这个之后,我意识到这与GlobalHost.DependencyResolver我提供给它的那个不一样HubConfiguration.一旦我加入
GlobalHost.DependencyResolver = config.Resolver;
Run Code Online (Sandbox Code Playgroud)
之前
app.MapSignalR("", config);
Run Code Online (Sandbox Code Playgroud)
我现在正在收到回调CloseRequestedCallback.不幸的是,一旦我从客户端调用方法到服务器,我就会收到以下错误:
Microsoft.AspNet.SignalR.Client.Infrastructure.SlowCallbackException
检测到可能的死锁.使用"HubProxy.On"或"Connection.Received"注册的回调已执行至少10秒.
我不确定我找到的修复程序以及它可能对系统产生的影响.GlobalHost.DependencyResolver没有注册所有默认内容,可以替换我自己的吗?
编辑2 09/10/15
根据这个,改变GlobalHost.DependencyResolver是做正确的事.SlowCallbackException由于我在所有回调中都没有做任何事情,但仍然没有解释.
问题一:IoC容器+依赖注入
如果您想为您更改 IoC HubConfiguration,您还需要更改 中的 IoC GlobalHost,以便在上下文之外请求它时返回相同的集线器。
问题 2:意外的 SlowCallbackException
这个异常是由于我在控制台应用程序中使用 SignalR 引起的。应用程序的入口点不能是async方法,因此为了能够异步调用我的初始配置,我执行了以下操作:
private static int Main()
{
var t = InitAsync();
t.Wait();
return t.Result;
}
Run Code Online (Sandbox Code Playgroud)
对我来说不幸的是,这会导致很多问题,如此处所述以及更多详细信息此处。
InitAsync通过如下方式启动我的:
private static int Main()
{
Task.Factory.StartNew(async ()=> await InitAsync());
m_waitInitCompletedRequest.WaitOne(TimeSpan.FromSeconds(30));
return (int)EndpointErrorCode.Ended;
}
Run Code Online (Sandbox Code Playgroud)
现在一切都运行良好,我没有遇到任何僵局。
有关问题和答案的更多详细信息,您还可以参考我的问题中的编辑。
| 归档时间: |
|
| 查看次数: |
1625 次 |
| 最近记录: |