Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

dependency injection - How to configure Autofac and SignalR in a MVC 5 application

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

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.

EDIT

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.

This is my startup class:

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

Here's how I do it:

var resolver = new AutofacDependencyResolver(container);

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

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

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.

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.

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...