我似乎无法使用以下代码。我正在进行ConfigureServies
调用_serviceProvider.GetService<IConnectionManager>();
并将其保存在静态字段中并稍后尝试使用它来访问IConnectionManager
并随后调用GetHubContext<MyHub>
所以我可以向所有连接的客户端广播消息。
_connectionManager.GetHubContext<MyHub>().Clients.All.doSomethingOnClients();
就像测试一样,webapi控制器动作方法中的同一行代码工作正常! (通过构造函数注入IConnectionManager)。这让我相信我的信号器设置很好,就像我在启动类中的东西在某处错了。启动时GetHashCode
上的IConnectionManager
和我的控制器提供了不同的哈希码。我只需要在ApplicationLifetime OnStartUp上挂钩......
你能帮我理解哪里出了问题吗?
public class Startup
{
public static IServiceProvider _serviceProvider;
public static IConnectionManager _connectionManager;
private readonly IHostingEnvironment _hostingEnv;
public IConfigurationRoot Configuration { get; }
public Startup (IHostingEnvironment env)
{
// ...
}
public void ConfigureServices (IServiceCollection services)
{
// ....
services.AddSignalR(options => {
options.Hubs.EnableDetailedErrors = true;
});
services.AddMvc();
// ...
_serviceProvider = services.BuildServiceProvider();
_connectionManager = _serviceProvider.GetService<IConnectionManager>();
}
public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime applicationLifetime)
{
// ...
applicationLifetime.ApplicationStarted.Register(OnStartUp);
// ...
app.UseMvc(routes => {
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
app.UseSignalR();
// ...
}
public void OnStartUp ()
{
var x = _serviceProvider.GetService<MySingletonObject>();
// MySingletonObject has a VersionUpdated event handler
x.VersionUpdated += OnUpdate;
}
private void OnUpdate (object sender, EventArgs e)
{
// I get here everytime my singleton gets updated fine!
// but the following does not work
_connectionManager.GetHubContext<MyHub>().Clients.All.doSomethingOnClients();
}
}
我正在使用&#34; Microsoft.AspNetCore.SignalR.Server / 0.2.0-alpha1-22362&#34;。
答案 0 :(得分:4)
第一件事是要意识到这个版本的SignalR没有发货,它只是alpha。你遇到的问题是因为你正在建立2个服务提供商而且他们没有互相交谈。您调用BuildServiceProvider()
而不是将IConnectionManager
注入Configure方法。您还可以通过将依赖项直接注入configure然后在回调中使用它们来清理大量服务定位器模式。
public class Startup
{
public IConfigurationRoot Configuration { get; }
public Startup (IHostingEnvironment env)
{
// ...
}
public void ConfigureServices (IServiceCollection services)
{
// ....
services.AddSignalR(options => {
options.Hubs.EnableDetailedErrors = true;
});
services.AddMvc();
// ...
}
public void Configure (IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
IApplicationLifetime applicationLifetime,
MySingletonObject obj,
IHubContext<MyHub> context)
{
// ...
applicationLifetime.ApplicationStarted.Register(() => OnStartUp(obj, context));
// ...
app.UseMvc(routes => {
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
app.UseSignalR();
// ...
}
public void OnStartUp(MySingletonObject obj, IHubContext<MyHub> context)
{
// MySingletonObject has a VersionUpdated event handler
obj.VersionUpdated += (sender, e) =>
{
context.Clients.All.doSomethingOnClients();
};
}
}
即使是更干净的也是另一种可以组成一切的服务,因此你的启动方法中不会有这么多的参数。