【问题标题】:Autofac injection of IHubContext SignalR MVCIHubContext SignalR MVC 的 Autofac 注入
【发布时间】:2016-06-19 22:08:30
【问题描述】:

我正在尝试让 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();
    }

我从 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

【问题讨论】:

    标签: asp.net-mvc asp.net-web-api signalr autofac


    【解决方案1】:

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

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

    然后,我为将要注入此集线器的对象设置特定的注册。这可能是 ApiController,也可能是其他一些服务,然后使用标准 Autofac/WebApi 集成将其注入控制器。这个“特定”注册是我不喜欢的部分,但我不知道有什么更干净的方法。这就是它的样子:

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

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

    【讨论】:

    • 谢谢山姆。您是否需要将命名属性添加到集线器本身?还是使用类名?
    • 它应该使用您在注册时在 GetHubContext() 调用中使用的类。我相信你不需要向集线器本身添加任何东西。
    • 如果那个控制器有另外两个参数怎么办?即记录器和回购?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    相关资源
    最近更新 更多