OWIN + SignalR + Autofac [英] OWIN + SignalR + Autofac
问题描述
来自: http://docs.autofac.org/en/latest /integration/signalr.html :
"OWIN集成中的一个常见错误是使用GlobalHost.在OWIN中,您是从头开始创建配置的.使用OWIN集成时,您不应在任何地方引用GlobalHost."
"A common error in OWIN integration is use of the GlobalHost. In OWIN you create the configuration from scratch. You should not reference GlobalHost anywhere when using the OWIN integration."
听起来很合理.但是,应该如何像通常的(非OWIN)一样,从ApiController解析IHubContext
:
That sounds reasonable. However, how should one resolve IHubContext
from an ApiController, like the usual (non-OWIN):
GlobalHost.ConnectionManager.GetHubContext<MyHub>()
?
我在任何地方都找不到此引用,而目前唯一的方法是在同一容器中注册HubConfiguration
实例并执行以下操作:
I can't find a reference on this one anywhere, and the only method I have by now is to register the HubConfiguration
instance within the same container and do this:
public MyApiController : ApiController {
public HubConfiguration HubConfig { get; set; } // Dependency injected by
// PropertiesAutowired()
public IHubContext MyHubContext {
get {
return HubConfig
.Resolver
.Resolve<IConnectionManager>()
.GetHubContext<MyHub>();
}
}
// ...
}
但是,这对我来说似乎很冗长.正确的做法是什么?更具体地说,是否有一种简单的方法来注册IConnectionManager
?
However, this seems quite verbose to me. What is the proper way to do it? To be more specific, is there a clean way to register IConnectionManager
?
我最终要做的事情是这样的:
What I ended up doing is something like:
var container = builder.Build();
hubConfig.Resolver = new AutofacDependencyResolver(container);
app.MapSignalR("/signalr", hubConfig);
var builder2 = new ContainerBuilder();
builder2
.Register(ctx => hubConfig.Resolver.Resolve<IConnectionManager>())
.As<IConnectionManager>();
builder2.Update(container);
但是我感觉必须有一种更简单的方法来将IConnectionManager
注入到控制器中.
but I have a feeling there must be an easier way to get that IConnectionManager
injected in the controller.
推荐答案
这个答案有点迟了,但是可以了.
This answer is a little belated but here goes.
- 我建议使用强类型的集线器.
- 您需要添加特定的 强类型集线器的注册.
- 我不使用
全球主机
- 相反,我使用为OWIN创建的配置 注册.
- I recommend strongly typed hubs.
- You need to add specific registrations for the strongly typed hubs.
- I don't use the
GlobalHost
- Instead I use the Configuration created for OWIN registration.
public interface IMyHub { // Any methods here for strongly-typed hubs } [HubName("myHub")] public class MyHub : Hub<IMyHub>
集线器注册
通过您的Autofac注册
Hub Registration
From your Autofac registration
// SignalR Configuration var signalRConfig = new HubConfiguration(); var builder = // Create your normal AutoFac container here builder.RegisterType<MyHub>().ExternallyOwned(); // SignalR hub registration // Register the Hub for DI (THIS IS THE MAGIC LINE) builder.Register(i => signalRConfig.Resolver.Resolve<IConnectionManager>().GetHubContext<MyHub, IMyHub>()).ExternallyOwned(); // Build the container var container = builder.Build(); // SignalR Dependency Resolver signalRConfig.Resolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container); app.UseAutofacMiddleware(container); app.MapSignalR("/signalr", signalRConfig);
在后台代码中解析集线器
使用AutoFacs AutowiredProperties()扩展方法,它可以解析正确的上下文(如果需要,也可以在构造函数中).
Resolving the hub in background code
Using AutoFacs AutowiredProperties() extension method then it can resolve the correct context (can also be in the constructor if you like).
public IHubContext<IMyHub> InstanceHubContext { get; [UsedImplicitly] set; }
这篇关于OWIN + SignalR + Autofac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!