如何在 MVC 5 应用程序中配置 Autofac 和 SignalR [英] How to configure Autofac and SignalR in a MVC 5 application

查看:27
本文介绍了如何在 MVC 5 应用程序中配置 Autofac 和 SignalR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置 MVC 5 应用程序以使用 SignalR 2.2.x 并将服务注入我的 NotificationsHub.我们正在为 MVC 使用 Autofac,但我不确定如何正确配置它.NuGet Autofac.Integration.SignalR (3.0.2)Autofac.Integration.Mvc (3.3.4) 存在 Autofac 文档.

I am trying to configure an MVC 5 application to use SignalR 2.2.x and inject a service into my NotificationsHub. We are using Autofac for MVC but I am not sure on how to correctly configure this. Autofac documentation exists for NuGet Autofac.Integration.SignalR (3.0.2) and Autofac.Integration.Mvc (3.3.4).

到目前为止我正在做的是通过以下方式注册集线器:

What I am doing so far is register the hubs via:

ContainerBuilder builder = new ContainerBuilder();

// Register MVC controllers.
builder.RegisterControllers(typeof(MvcApplication).Assembly);

builder.RegisterType<ServiceForSignalRHub>().AsImplementedInterfaces();
builder.RegisterType<...>().AsImplementedInterfaces();

builder.RegisterHubs(Assembly.GetExecutingAssembly());

IContainer container = builder.Build();

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

然而,对 .SetResolver(...) 的调用是模棱两可的,因为它存在于 MVC 和 SignalR 集成包中.怎么办?我无法确认我的服务的构造函数注入是否适用于我正在使用的集线器.

However the call to .SetResolver(...) is ambigious as it exists in both MVC and SignalR integration packages. Now what? I am unable to confirm if the contructor injection of my service works for the Hub I am using.

编辑

我可以用 Autofac 配置 MVC5 就好了.我迷路的地方是如何使用 SignalR 做同样的事情,使用 Autofac Signal 集成 NuGet.请参阅下文.

这是我的创业班:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var defaultFactory = LogManager.Use<DefaultFactory>();
        defaultFactory.Directory("@D:\NServiceBus\ );
        defaultFactory.Level(LogLevel.Debug);

        var container = ConfigureAutofac(app);

        ConfigureServiceBus(container, app);

        app.UseAutofacMiddleware(container); // REGISTER WITH OWIN
        app.UseAutofacMvc();

        ConfigureAuth(app);

        GlobalConfiguration.Configuration.UseSqlServerStorage("hangfire");

        ConfigureSignalR(app);
    }
}

这是我配置 SignalR 的部分:

This is the part where I configure SignalR:

public partial class Startup
{
    public void ConfigureSignalR(IAppBuilder app)
    {
        var builder = new ContainerBuilder();

        builder.RegisterHubs(Assembly.GetExecutingAssembly()).PropertiesAutowired();
        builder.RegisterType<MaintenanceService>().As<IMaintenanceService>().PropertiesAutowired();

        var container = builder.Build();
        //var container = new AutofacContainer().Container;

        var resolver = new AutofacDependencyResolver(container);

        // Any connection or hub wire up and configuration should go here
        app.MapSignalR(new HubConfiguration
        {
            Resolver = resolver,
            EnableJSONP = true,
            EnableDetailedErrors = true,
            EnableJavaScriptProxies = true
        });

        builder.RegisterInstance(resolver.Resolve<IConnectionManager>());
    }
}

如何才能正确地做到这一点?

How can this be correctly done?

推荐答案

您应该在映射时明确告诉 SignalR 使用 AutofacDependencyResolver.我假设你知道在你的 Startup 类中你必须调用 app.MapSignalR();

You should tell SignalR explicitly to use the AutofacDependencyResolver when you are mapping it. I assume that you know that in your Startup class you have to call app.MapSignalR();

当你映射它时,你应该告诉它使用自定义依赖解析器(AutofacDependencyResolver).

When you are mapping it, you should tell it to use the custom dependency resolver (the AutofacDependencyResolver).

这是我的做法:

var resolver = new AutofacDependencyResolver(container);

app.MapSignalR(new HubConfiguration
{
    Resolver = resolver
});

通过这种方式,您可以直接告诉 SignalR 使用哪个依赖项解析器.

This way, you are telling SignalR directly which dependency resolver to use.

我有一个 用于 SignalR 依赖注入的 GitHub 存储库,但它没有配置为使用 MVC.不过,我认为它会给你一个关于如何创建配置的提示.

I have a GitHub repo for SignalR Dependency Injection, but it's not configured to use MVC. Still, I think it will give you a hint in how to create your configuration.

注意:如果您使用的是 OWIN 中间件,请务必不要使用 GlobalHost 静态属性,因为它会产生大量的不一致.

Note: If you are using the OWIN Middleware, be sure not to use the GlobalHost static property AT ALL since it will have massive inconsistencies.

OWIN 集成中的一个常见错误是使用 GlobalHost.在欧文您从头开始创建配置.你不应该参考使用 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.

再次检查我给你的 repo,看看如何做到这一点.

Again, check the repo I gave you to see how to do this.

希望这有帮助:)祝你好运!

Hope this helps:) Best of luck!

这篇关于如何在 MVC 5 应用程序中配置 Autofac 和 SignalR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆