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

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

问题描述

我正在尝试配置MVC 5应用程序以使用SignalR 2.2.x并将服务注入到我的NotificationsHub中. 我们正在将Autofac用于MVC,但是我不确定如何正确配置它. 已经存在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.我迷路的是如何使用Autofac Signal集成NuGet对SignalR进行同样的操作. 请参阅下文.

I can configure MVC5 with Autofac just fine. Where I get lost is how to do the same with SignalR, using the Autofac Signal integration NuGet. Please see below.

这是我的入门班:

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>());
    }
}

如何正确完成?

推荐答案

在映射它时,应明确告诉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静态属性AT ALL,因为它会产生巨大的不一致.

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中 您可以从头开始创建配置.你不应该参考 使用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.

再次,检查我给您的回购,以了解如何执行此操作.

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天全站免登陆