To get Autofac integrated with SignalR you need to reference the SignalR integration NuGet package, register your hubs, and set the dependency resolver.

protectedvoidApplication_Start(){varbuilder=newContainerBuilder();// Register your SignalR hubs.builder.RegisterHubs(Assembly.GetExecutingAssembly());// Set the dependency resolver to be Autofac.varcontainer=builder.Build();GlobalHost.DependencyResolver=newAutofacDependencyResolver(container);}

The sections below go into further detail about what each of these features do and how to use them.

At application startup, while building your Autofac container, you should register your SignalR hubs and their dependencies. This typically happens in an OWIN startup class or in the Application_Start method in Global.asax.

varbuilder=newContainerBuilder();// You can register hubs all at once using assembly scanning...builder.RegisterHubs(Assembly.GetExecutingAssembly());// ...or you can register individual hubs manually.builder.RegisterType<ChatHub>().ExternallyOwned();

If you register individual hubs, make sure they are registered as ExternallyOwned(). This ensures SignalR is allowed to control disposal of the hubs rather than Autofac.

After building your container pass it into a new instance of the AutofacDependencyResolver class. Attach the new resolver to your GlobalHost.DependencyResolver (or HubConfiguration.Resolver if you’re using OWIN) to let SignalR know that it should locate services using the AutofacDependencyResolver. This is Autofac’s implementation of the IDependencyResolver interface.

Any components registered as InstancePerLifetimeScope() will effectively be singletons. Given there’s one root lifetime scope, you’ll only get the one instance.

To make managing your hub dependency lifetimes easier you can have the root lifetime scope injected into the constructor of your hub. Next, create a child lifetime scope that you can use for the duration of your hub invocation and resolve the required services. Finally, make sure you dispose the child lifetime when the hub is disposed by SignalR. (This is similar to service location, but it’s the only way to get a “per-hub” sort of scope. No, it’s not awesome.)

publicclassMyHub:Hub{privatereadonlyILifetimeScope_hubLifetimeScope;privatereadonlyILogger_logger;publicMyHub(ILifetimeScopelifetimeScope){// Create a lifetime scope for the hub._hubLifetimeScope=lifetimeScope.BeginLifetimeScope();// Resolve dependencies from the hub lifetime scope._logger=_hubLifetimeScope.Resolve<ILogger>();}publicvoidSend(stringmessage){// You can use your dependency field here!_logger.Write("Received message: "+message);Clients.All.addMessage(message);}protectedoverridevoidDispose(booldisposing){// Dipose the hub lifetime scope when the hub is disposed.if(disposing&&_hubLifetimeScope!=null){_hubLifetimeScope.Dispose();}base.Dispose(disposing);}}

If this is a common pattern in your application, you might consider creating a base/abstract hub from which other hubs can derive to save all the copy/paste creation/disposal of scopes.

Injecting a lifetime scope into your hub does not give you per-request lifetime scopes. It just gives you a way to manage dependency lifetime in a more active way than resolving everything from the root container. Using InstancePerRequest, even with this workaround, will still fail. You may want to read the FAQ on per-request scope for more info.

publicclassStartup{publicvoidConfiguration(IAppBuilderapp){varbuilder=newContainerBuilder();// STANDARD SIGNALR SETUP:// Get your HubConfiguration. In OWIN, you'll create one// rather than using GlobalHost.varconfig=newHubConfiguration();// Register your SignalR hubs.builder.RegisterHubs(Assembly.GetExecutingAssembly());// Set the dependency resolver to be Autofac.varcontainer=builder.Build();config.Resolver=newAutofacDependencyResolver(container);// OWIN SIGNALR SETUP:// Register the Autofac middleware FIRST, then the standard SignalR middleware.app.UseAutofacMiddleware(container);app.MapSignalR("/signalr",config);// To add custom HubPipeline modules, you have to get the HubPipeline// from the dependency resolver, for example:varhubPipeline=config.Resolver.Resolve<IHubPipeline>();hubPipeline.AddModule(newMyPipelineModule());}}