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
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SignalR.Extras.Autofac" Version="1.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SignalR.Extras.Autofac --version 1.3.0
#r "nuget: SignalR.Extras.Autofac, 1.3.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

Build & Publish NuGet GitHub license

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 or LifetimeHub<T>

But in more detail:

  1. Install the NuGet package: SignalR.Extras.Autofac

  2. Reference the namespace:

using SignalR.Extras.Autofac;
  1. Call the new RegisterLifetimeHubManager extension method on your ContainerBuilder instance, e.g.:
builder.RegisterLifetimeHubManager();
  1. 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().

  2. Ensure that your SignalR hubs which require automatic lifetime scope management (it'll be per hub-invocation) inherit from the LifetimeHub or LifetimeHub<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 Compatible and additional computed target framework versions.
.NET Framework net47 is compatible.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 986 8/9/2023
1.2.1-ci0019 297 8/9/2023
1.2.0 87,261 4/19/2016
1.1.1 3,027 9/10/2015
1.1.0 1,345 9/9/2015
1.0.0 1,833 1/18/2015
0.2.1-beta 1,200 7/11/2014
0.2.0-beta 1,388 7/5/2014