• Stars
    star
    101
  • Rank 327,645 (Top 7 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Enables multitenant dependency injection support for ASP.NET Core.

Autofac.AspNetCore.Multitenant

ASP.NET Core support for multitenant DI via Autofac.Multitenant.

Build status codecov

Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.

Why Use This?

ASP.NET Core default RequestServicesContainerMiddleware is where the per-request lifetime scope usually gets generated. However, its constructor is where it wants the IServiceScopeFactory that will be used later during the request to create the request lifetime scope.

Unfortunately, that means the IServiceScopeFactory is created/resolved at the point when the request comes in, long before an HttpContext is set in any IHttpContextAccessor. The result is the scope factory ends up coming from the default tenant scope, before a tenant can be identified, and per-request services will later all come from the default tenant. Multitenancy fails.

This package provides a different request services middleware that ensures the IHttpContextAccessor.HttpContext is set and defers creation of the request lifetime scope until as late as possible so anything needed for tenant identification can be established.

Quick Start

When creating your application host, use the multitenant service provider factory. Your Startup will register common dependencies, but you'll need to provide a static method to initialize the tenant-specific overrides.

var host = Host.CreateDefaultBuilder(args)
    .UseServiceProviderFactory(new AutofacMultitenantServiceProviderFactory(MultitenantContainerSetup.ConfigureMultitenantContainer))
    .ConfigureWebHostDefaults(webHostBuilder => webHostBuilder.UseStartup<Startup>())
    .Build();

In your Startup class, make sure to use the multitenant request services middleware and register your common dependencies.

public class Startup
{
  public void ConfigureServices(IServiceCollection services)
  {
    // Add the multitenant request services handler.
    services
      .AddAutofacMultitenantRequestServices()
      .AddControllers();
  }

  public void ConfigureContainer(ContainerBuilder builder)
  {
    // Register tenant-shared dependencies and defaults.
    builder.RegisterType<CommonDependency>()
      .As<IDependency>()
      .InstancePerLifetimeScope();
  }

  public void Configure(IApplicationBuilder app)
  {
      app.UseRouting();
      app.UseEndpoints(builder => builder.MapControllers());
  }
}

Provide a method to override things for tenant-specific dependencies. This is what gets passed to the multitenant service provider factory.

public static class MultitenantContainerSetup
{
  public static MultitenantContainer ConfigureMultitenantContainer(IContainer container)
  {
    // Define how you're going to identify tenants.
    var strategy = new QueryStringTenantIdentificationStrategy(
        container.Resolve<IHttpContextAccessor>(),
        container.Resolve<ILogger<QueryStringTenantIdentificationStrategy>>());

    // Create the multitenant container.
    var multitenantContainer = new MultitenantContainer(strategy, container);

    // Register tenant overrides.
    multitenantContainer.ConfigureTenant(
        "some-tenant",
        cb => cb
          .RegisterType<OverrideDependency>()
          .As<IDependency>()
          .WithProperty("Id", "some-tenant")
          .InstancePerLifetimeScope());

    // Return the built container for use in the app.
    return multitenantContainer;
  }
}

Reference

More Repositories

1

Autofac

An addictive .NET IoC container
C#
4,256
star
2

Examples

Example projects that consume and demonstrate Autofac IoC functionality and integration
C#
379
star
3

Autofac.Extensions.DependencyInjection

Autofac implementation of the interfaces in Microsoft.Extensions.DependencyInjection.Abstractions, the .NET Core dependency injection abstraction.
C#
171
star
4

Autofac.Extras.DynamicProxy

Interceptor and decorator support for Autofac IoC via Castle DynamicProxy
C#
102
star
5

Documentation

Usage and API documentation for Autofac and integration libraries
JavaScript
66
star
6

Autofac.Mvc

ASP.NET MVC integration for Autofac
C#
47
star
7

Autofac.Configuration

Configuration support for Autofac IoC
C#
36
star
8

Autofac.Extras.Moq

Moq auto mocking integration for Autofac IoC
C#
35
star
9

Autofac.Multitenant

Multitenant application support for Autofac IoC
C#
35
star
10

Autofac.WebApi

ASP.NET Web API integration for Autofac
C#
35
star
11

Autofac.ServiceFabric

Autofac integration for Azure Service Fabric. Provides service factory implementations for Actors, Stateful Services and Stateless Services.
C#
25
star
12

Autofac.Owin

OWIN integration for Autofac
C#
22
star
13

Autofac.Wcf

Windows Communication Foundation (WCF) integration for Autofac IoC
C#
20
star
14

Autofac.Mef

Managed Extensibility Framework (MEF) integration for Autofac IoC
C#
19
star
15

Autofac.Web

ASP.NET WebForms integration for Autofac
C#
10
star
16

Autofac.SignalR

SignalR integration for Autofac IoC
PowerShell
10
star
17

Autofac.AspNetCore

Autofac extensions and helpers for ASP.NET Core
PowerShell
9
star
18

Autofac.WebApi.Owin

OWIN support for the ASP.NET Web API integration for Autofac
C#
8
star
19

Autofac.Extras.CommonServiceLocator

Common Service Locator implementation for Autofac IoC
PowerShell
7
star
20

Autofac.Extras.FakeItEasy

FakeItEasy auto mocking integration for Autofac IoC
C#
7
star
21

Autofac.Extras.NHibernate

Autofac implementation of the NHibernate factories
C#
6
star
22

Autofac.Extras.MvvmCross

MvvmCross integration for Autofac IoC
C#
5
star
23

Autofac.Extras.AggregateService

Dynamic aggregate service implementation generation for Autofac IoC
C#
4
star
24

Autofac.Multitenant.Wcf

Multitenant Windows Communication Foundation (WCF) enhancements for Autofac IoC
C#
3
star
25

Autofac.Pooling

Support for pooled instance lifetime scopes in Autofac dependency injection.
C#
3
star
26

Autofac.Extras.AttributeMetadata

Attribute metadata support for Autofac IoC
C#
3
star
27

Autofac.Bot.Api

Handlers for Autofac Bot commands that can be used to execute various Autofac tasks like running benchmarks.
C#
2
star
28

Autofac.Extensions.Hosting

Fluent configuration of Autofac with the Microsoft.Extensions.Hosting package
PowerShell
2
star
29

Autofac.Mvc.Owin

OWIN support for the ASP.NET MVC integration for Autofac
PowerShell
2
star
30

Autofac.Diagnostics.DotGraph

Autofac diagnostics support to enable DOT graph visualization of resolve requests.
C#
2
star
31

autofac-bot

GitHub application based on Probot for executing common Autofac-based tasks.
TypeScript
1
star
32

Autofac.Analyzers

Roslyn code analyzers to help with Autofac usage.
C#
1
star
33

Autofac.Extras.DomainServices

Autofac Domain Service Factory for RIA Services
C#
1
star