Skip to content

ASP.NET Core library with IWebHostBuilder Single Tenant and Multitenant Autofac Extensions

License

Notifications You must be signed in to change notification settings

davidikin45/Autofac.AspNetCore.Extensions

Repository files navigation

Autofac ASP.NET Core Extensions

nuget Downloads

Installation

NuGet

PM> Install-Package Autofac.AspNetCore.Extensions

.Net CLI

> dotnet add package Autofac.AspNetCore.Extensions

Nuget Packages

  • Autofac.Extensions.DependencyInjection
  • Autofac.Multitenant
  • Autofac.AspNetCore.Multitenant

Single Tenant Usage ASP.NET Core 2.2

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseAutofac()
.UseStartup<Startup>();

Single Tenant Usage ASP.NET Core 3.0

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
	webBuilder
	.UseAutofac()
	.UseStartup<Startup>();
});

Multitenant Usage ASP.NET Core 2.2

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
	services.AddHttpContextAccessor();
	services.AddScoped<ITenantIdentificationStrategy, SubdomainIdentificationStrategy>();
})
.UseAutofacMultitenant()
.UseStartup<Startup>();

public class SubdomainIdentificationStrategy : ITenantIdentificationStrategy
{
	private readonly IHttpContextAccessor _httpContextAccessor;
	public SubdomainIdentificationStrategy(IHttpContextAccessor httpContextAccessor)
	{
		_httpContextAccessor = httpContextAccessor;
	}

	public bool TryIdentifyTenant(out object tenantId)
	{
		var context = _httpContextAccessor.HttpContext;
		if(context == null)
		{
			tenantId = null;
			return false;
		}

		tenantId = GetSubDomain(context);
		return tenantId != null;
	}

	private string GetSubDomain(HttpContext httpContext)
	{
		var subDomain = string.Empty;

		var host = httpContext.Request.Host.Host;

		if (!string.IsNullOrWhiteSpace(host))
		{
			subDomain = host.Split('.')[0];
		}

		return subDomain.Trim().ToLower();
	}
}

Multitenant Usage ASP.NET Core 3.0

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
	webBuilder
	.ConfigureServices(services =>
	{
		services.AddHttpContextAccessor();
		services.AddScoped<ITenantIdentificationStrategy, SubdomainIdentificationStrategy>();
	})
	.UseAutofacMultitenant()
	.UseStartup<Startup>();
});

public class SubdomainIdentificationStrategy : ITenantIdentificationStrategy
{
	private readonly IHttpContextAccessor _httpContextAccessor;
	public SubdomainIdentificationStrategy(IHttpContextAccessor httpContextAccessor)
	{
		_httpContextAccessor = httpContextAccessor;
	}

	public bool TryIdentifyTenant(out object tenantId)
	{
		var context = _httpContextAccessor.HttpContext;
		if(context == null)
		{
			tenantId = null;
			return false;
		}

		tenantId = GetSubDomain(context);
		return tenantId != null;
	}

	private string GetSubDomain(HttpContext httpContext)
	{
		var subDomain = string.Empty;

		var host = httpContext.Request.Host.Host;

		if (!string.IsNullOrWhiteSpace(host))
		{
			subDomain = host.Split('.')[0];
		}

		return subDomain.Trim().ToLower();
	}
}

Authors

License

This project is licensed under the MIT License

About

ASP.NET Core library with IWebHostBuilder Single Tenant and Multitenant Autofac Extensions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages