自动注射IHubContext SignalR MVC

set*_*dnz 2 asp.net-mvc autofac signalr asp.net-web-api

我想让SignalR与Autofac合作.我有一个我在这里所做的剥离版本的回购:

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

这是使用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();
    }
Run Code Online (Sandbox Code Playgroud)

我从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);
    }

}
Run Code Online (Sandbox Code Playgroud)

我有一个控制器,它注入到它的contstructor:

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

        // using DI
        _context = hubContext;
    }
Run Code Online (Sandbox Code Playgroud)

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

任何帮助都会很棒.我已经回复了我正在谈论的内容,可以在这里找到一个完整的解决方案:

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

Sam*_*rie 5

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

builder.Register(ctx => 
    ctx.Resolve<IDependencyResolver>()
       .Resolve<IConnectionManager>()
       .GetHubContext<EventHub>())
       .Named<IHubContext>("EventHub");
Run Code Online (Sandbox Code Playgroud)

然后我为我将注入此集线器的对象设置了特定的注册.这可以是ApiController,也可以是其他一些服务,然后使用标准的Autofac/WebApi集成注入控制器.这个"特定"的注册是我不喜欢的部分,但我不知道更清洁的方式.这是什么样子:

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

现在,Autofac应该认识到你想要注入IHubContext TaskController并在注入时提供名为EventHub的特定注册.