SignalR.Extras.Autofac
1.3.0
dotnet add package SignalR.Extras.Autofac --version 1.3.0
NuGet\Install-Package SignalR.Extras.Autofac -Version 1.3.0
<PackageReference Include="SignalR.Extras.Autofac" Version="1.3.0" />
paket add SignalR.Extras.Autofac --version 1.3.0
#r "nuget: SignalR.Extras.Autofac, 1.3.0"
// Install SignalR.Extras.Autofac as a Cake Addin #addin nuget:?package=SignalR.Extras.Autofac&version=1.3.0 // Install SignalR.Extras.Autofac as a Cake Tool #tool nuget:?package=SignalR.Extras.Autofac&version=1.3.0
SignalR.Extras.Autofac
This library is designed for use with SignalR 2.x in the .NET Framework.
Adds support for per-request lifetime scopes for SignalR hub dependencies when using Autofac as your IoC container.
SignalR already creates a separate hub instance for each call, and Autofac is able to inject any dependencies, but neither library provides a built-in way for those dependencies lives to be scoped to the hub's lifetime / requests.
SignalR.Extras.Autofac provides a simple, transparent way to bridge that gap, effectively allowing per-request dependency injection for SignalR hubs. You just need to register the LifetimeHubManager
with Autofac and ensure your hubs inherit from LifetimeHub
.
Getting Started:
The TLDR is there are basically two things you need to do, beyond the standard Autofac & SignalR integration steps:
- Call the
RegisterLifetimeHubManager()
extension method on your Autofac container builder - Ensure your hubs inherit from
LifetimeHub
orLifetimeHub<T>
But in more detail:
Install the NuGet package: SignalR.Extras.Autofac
Reference the namespace:
using SignalR.Extras.Autofac;
- Call the new RegisterLifetimeHubManager extension method on your ContainerBuilder instance, e.g.:
builder.RegisterLifetimeHubManager();
When setting up an Autofac container in your project, follow the usual Autofac & SignalR integration steps as outlined on the Autofac wiki (https://autofac.readthedocs.io/en/v6.0.0/integration/signalr.html), i.e. replace SignalR's dependency resolver with Autofac's custom one and register your hubs as you normally would. If you're registering your hubs manually, you still need to configure the registrations with ExternallyOwned().
Ensure that your SignalR hubs which require automatic lifetime scope management (it'll be per hub-invocation) inherit from the
LifetimeHub
orLifetimeHub<T>
classes.
Your hub instances will automatically and transparently be assigned their own new child lifetime scopes upon each invocation by SignalR. They will also automatically dispose of those lifetime scopes upon completion. You do not need to manage the disposal of those hubs which inherit from LifetimeHub
or LifetimeHub<T>
, or the injected dependencies owned by Autofac.
You can still register and use Hubs which do not inherit from LifetimeHub
or LifetimeHub<T>
. Dependencies will still be injected correctly by Autofac, however you will have to manually manage their lifetime scopes yourself (as described here https://autofac.readthedocs.org/en/v6.0.0/integration/signalr.html#managing-dependency-lifetimes).
Note: Disposing the Autofac container will result in any tracked LifetimeHub instances (even if they're ExternallyOwned), and their scoped dependencies, also being disposed at that time.
Examples:
Registration on an IIS host
// Create the container builder.
var builder = new ContainerBuilder();
// IMPORTANT: Register the LifetimeHubManager - this is required to support per-request hub dependencies
builder.RegisterLifetimeHubManager();
// Register the SignalR hubs.
builder.RegisterHubs(Assembly.GetExecutingAssembly()); //Register all hubs in an assembly
// Register other dependencies.
builder.Register(c => new UnitOfWork()).As<IUnitOfWork>().InstancePerLifetimeScope();
// Build the container.
var container = builder.Build();
// Set Autofac as SignalR's dependency resolver
GlobalHost.DependencyResolver = new AutofacDependencyResolver(container);
Registration on an OWIN host
// Create the container builder.
var builder = new ContainerBuilder();
// IMPORTANT: Register the LifetimeHubManager - this is required to support per-request hub dependencies
builder.RegisterLifetimeHubManager();
// Register the SignalR hubs.
builder.RegisterHubs(Assembly.GetExecutingAssembly()); //Register all hubs in an assembly
// Register other dependencies.
builder.Register(c => new UnitOfWork()).As<IUnitOfWork>().InstancePerLifetimeScope();
// Build the container.
var container = builder.Build();
// Register the Autofac middleware FIRST, then the standard SignalR middleware.
app.UseAutofacMiddleware(container);
app.MapSignalR("/signalr", new HubConfiguration {
// Set Autofac as SignalR's dependency resolver
Resolver = new AutofacDependencyResolver(container)
});
Your hubs
public class MyHub : LifetimeHub
{
public MyHub(IUnitOfWork unitOfWork) {
_unitOfWork = unitOfWork;
}
public void DoSomething() {
//The hub instance and dependencies like UnitOfWork are automatically created prior to SignalR invoking this method
//Do stuff here
//The hub instance and dependencies like UnitOfWork are automatically destroyed after SignalR has invoked this method
}
private IUnitOfWork _unitOfWork;
}
public class SomeHub : LifetimeHub<ISomeClient>
{
public SomeHub(IUnitOfWork unitOfWork) {
_unitOfWork = unitOfWork;
}
public void DoSomething() {
//The hub instance and dependencies like UnitOfWork are automatically created prior to SignalR invoking this method
//Do stuff here
//The hub instance and dependencies like UnitOfWork are automatically destroyed after SignalR has invoked this method
}
private IUnitOfWork _unitOfWork;
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net47 is compatible. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.7
- Autofac (>= 6.5.0)
- Microsoft.AspNet.SignalR.Core (>= 2.4.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.3.0 | 1,288 | 8/9/2023 |
1.2.1-ci0019 | 322 | 8/9/2023 |
1.2.0 | 93,986 | 4/19/2016 |
1.1.1 | 3,077 | 9/10/2015 |
1.1.0 | 1,393 | 9/9/2015 |
1.0.0 | 1,879 | 1/18/2015 |
0.2.1-beta | 1,235 | 7/11/2014 |
0.2.0-beta | 1,421 | 7/5/2014 |