IHubContext SignalR MVC 的 Autofac 注入

Autofac injection of IHubContext SignalR MVC

我正在尝试让 SignalR 与 Autofac 一起工作。我有一个我在这里所做的精简版本的回购协议:

https://github.com/justsayno/signalr-autofac

改编自 which works using GlobalHost:

https://github.com/sstorie/experiments/tree/master/angular2-signalr

使用 hte GlobalHost 对象效果很好。我试图按照 Autofac 网站上有关如何注入 SignalR 服务的文档进行操作,但我无法使其正常工作。这是我用于注册依赖项的配置文件:

public static IContainer RegisterDependencies()
    {
        // Create your builder.
        var builder = new ContainerBuilder();

        //register controllers in DI
        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // Register SignalR hubs
        builder.RegisterHubs(Assembly.GetExecutingAssembly());

        return builder.Build();
    }

我从 startup.cs 中调用它:

public class Startup
{
    public static IContainer Container { get; set; }
    public void Configuration(IAppBuilder app)
    {
        var config = GlobalConfiguration.Configuration;

        // configure IoC container
        Container = AutofacConfiguration.RegisterDependencies();

        //set dependency resolver from WebAPI and MVC
        config.DependencyResolver = new AutofacWebApiDependencyResolver(Container);
        DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(Container));

        //register Autofac Middleware
        app.UseAutofacMiddleware(Container);

        app.Map("/signalr", a =>
        {
            a.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration
            {
                Resolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(Container)
            };
            a.RunSignalR(hubConfiguration);
        });

        // This server will be accessed by clients from other domains, so
        //  we open up CORS
        //
        app.UseCors(CorsOptions.AllowAll);

        // Build up the WebAPI middleware
        //
        var httpConfig = new HttpConfiguration();
        httpConfig.MapHttpAttributeRoutes();
        app.UseWebApi(httpConfig);
    }

}

我有一个控制器,它注入了它的构造函数:

 public TaskController(IHubContext hubContext)
    {
        // Commented out hard coded global host version
        //
        //_context = GlobalHost.ConnectionManager.GetHubContext<EventHub>();

        // using DI
        _context = hubContext;
    }

然而,这给了我一个关于控制器没有默认构造函数的错误(所以我认为这是我的 IHubContext 未找到的问题)。

任何帮助都会很棒。我已经对我正在谈论的内容进行了回购,可以在此处找到完整的解决方案:

https://github.com/justsayno/signalr-autofac

我已经能够做到这一点,但它不像我通常希望的那样干净。这里的主要问题是 IHubContext 不反映集线器的具体 类型 是什么......它只是一个通用句柄。所以我所做的是在 Autofac 中创建命名注册以使用特定的 SignalR 集线器注册 IHubContext:

builder.Register(ctx => 
    ctx.Resolve<IDependencyResolver>()
       .Resolve<IConnectionManager>()
       .GetHubContext<EventHub>())
       .Named<IHubContext>("EventHub");

然后我为将要注入此集线器的对象设置特定的注册。这可以是一个 ApiController,也可以是一些其他服务,然后使用标准 Autofac/WebApi 集成将其注入到控制器中。这个 "specific" 注册是我不喜欢的部分,但我不知道有什么更简洁的方法来解决它。这是它的样子:

builder.RegisterType<TaskController>()
       .WithParameter(
           new ResolvedParameter(
               (pi, ctx) => pi.ParameterType == typeof(IHubContext),
               (pi, ctx) => ctx.ResolveNamed<IHubContext>("EventHub")
       )
 );

现在 Autofac 应该识别出您想将 IHubContext 注入 TaskController 并在注入时提供名为 EventHub 的特定注册。