lethek / SignalR.Extras.Autofac

Adds support for per-request lifetime scopes for SignalR hub dependencies when using Autofac as your IoC container.
MIT License
10 stars 2 forks source link

How to make it work (without issues at resolving) #3

Open hidegh opened 7 years ago

hidegh commented 7 years ago

So, it's a nice solution, but making DI work was a pain. Found 2 good links:

Anyhow the solution is here...

**AutofacWebctivator**

            // Need to register SignalR Hubs (and special LifetimeHubManager too)!
            builder.RegisterLifetimeHubManager();
            builder.RegisterType<ExportPacketHub>().ExternallyOwned();

            //
            // SignalR MAGIC CODE for DI!
            builder.RegisterType<Autofac.Integration.SignalR.AutofacDependencyResolver>()
                .As<Microsoft.AspNet.SignalR.IDependencyResolver>()
                .SingleInstance();

            builder.Register(context =>
                context
                    .Resolve<Microsoft.AspNet.SignalR.IDependencyResolver>()
                    .Resolve<Microsoft.AspNet.SignalR.Infrastructure.IConnectionManager>()
                    .GetHubContext<ExportPacketHub>()
                )
                .ExternallyOwned();

            //
            // ...
            container = builder.Build();
**Startup.cs**

            // Grab container
            var container = AutofacWebActivator.Container;

            // Get your HubConfiguration. In OWIN, we create one rather than using GlobalHost
            var hubConfig = new HubConfiguration();

            // Sets the dependency resolver to be autofac (WITHOUT CONTAINERBUILDER UPDATE)...
            hubConfig.Resolver = container.Resolve<Microsoft.AspNet.SignalR.IDependencyResolver>();

            // OWIN SIGNALR SETUP: register the Autofac middleware FIRST, then the standard SignalR middleware.
            app.UseAutofacMiddleware(container);
            app.MapSignalR("/signalr", hubConfig);
lethek commented 7 years ago

Thanks - I've not used this with OWIN yet, just ASP.NET projects directly on IIS. I'll check it out later when I'm free; it's probably also a good opportunity to implement a demo project or two.

If I'm understanding correctly, the issue is that setting SignalR's dependency resolver via GlobalHost is the wrong thing to do with OWIN middleware (is ignored or overwritten by SignalR I guess?) and it should be done on a new HubConfiguration instance instead otherwise it can/will cause resolving problems?

hidegh commented 7 years ago

@lethek Hi, yes, according other links there's an overwrite issue. Must admit that I haven't checked myself. I had issues with resolving the hub, cause some bub related infrastructural typeswere not injected. So this is solved in the first code-snippet [this was the major issue I had]. The second one then solves the overwrite issue. Btw: I'm glad that AutoFac (unlike Microsoft - as usual of them) mentioned the problem of not using life-time-scopes! And even more happy that found this solution of yours :D Thanx. Hope my finding helps too.