Anto Subash.

Published on

Extend Tenant management and add custom host to your ABP App. Part 7

Table of contents

Intro

In this post we will see how to extent the tenant entity and Add a custom tenant resolver for your ABP app.

Add test domain in the host file

In windows the host file is located in C:\Windows\System32\drivers\etc\hosts. Open the file with the Admin privilege to update the below entry.

1127.0.0.1       test1.local
2127.0.0.1       test2.local
3127.0.0.1       test3.local

Create an abp App and Run Migrations

Create the App

Run the following command to create the abp app.

1abp new CustomHost

Run Migrations

change directory to src/CustomHost.DbMigrator and run the migration project

1dotnet run

This will create the migrations and seed the data for your project.

Extend Tenant Entity

Configure extra properties

Create the Constant file in the Domain.Shared project.

1public static class Constant
2{
3    public const string Host = "Host";
4}

In the Domain.Shared project update the CustomHostModuleExtensionConfigurator file inside the ConfigureExtraProperties methods with the following.

1ObjectExtensionManager.Instance.Modules()
2        .ConfigureTenantManagement(tenantConfig =>
3        {
4            tenantConfig.ConfigureTenant(tenant =>
5            {
6                tenant.AddOrUpdateProperty<string>(Constant.Host);
7            });
8        });

Configure ef core

In the EntityFrameworkCore project update the CustomHostEfCoreEntityExtensionMappings file inside the Configure method. Update the OneTimeRunner.Run with the following

1ObjectExtensionManager.Instance
2    .MapEfCoreProperty<Tenant, string>(Constant.Host);

Add Migrations

Navigate to the EntityFrameworkCore.

1cd .\src\CustomHost.EntityFrameworkCore\

Add migrations

1dotnet ef migrations add "update_tenant_with_host"

Apply the migrations

1dotnet ef database update

Add a custom Tenant Repo

Create a Custom Repo Interface in the Domain project

1public interface ICustomTenantRepository : IBasicRepository<Tenant, Guid>
2{
3    Task<Tenant> GetTenantByHost(string host, CancellationToken cancellationToken = default);
4}

Implement the custom tenant repo in the EntityFrameworkCore project

1public class CustomTenantRepository : EfCoreRepository<TenantManagementDbContext, Tenant, Guid>, ICustomTenantRepository
2{
3    public CustomTenantRepository(IDbContextProvider<TenantManagementDbContext> dbContextProvider) : base(dbContextProvider)
4    {
5    }
6
7    public async Task<Tenant> GetTenantByHost(string host, CancellationToken cancellationToken = default)
8    {
9        var context = await GetDbContextAsync();
10        var tenant =  context.Tenants.Where(u => EF.Property<string>(u, "Host") == host);
11        return await tenant.FirstOrDefaultAsync(cancellationToken: cancellationToken);
12    }
13}

Add the custom Repo to the DbContext

1options.AddRepository<Tenant, CustomTenantRepository>();

Implement Host Tenant Resolve Contributor

Create a file called HostTenantResolveContributor in the Domain Project

1public class HostTenantResolveContributor : TenantResolveContributorBase
2{
3    public override async Task ResolveAsync(ITenantResolveContext context)
4    {
5        var currentContextAccessor = context.ServiceProvider.GetRequiredService<IHttpContextAccessor>();
6        var tenantRepository = context.ServiceProvider.GetRequiredService<ICustomTenantRepository>();
7
8        var host = currentContextAccessor?.HttpContext?.Request.Host.Host;
9        if (tenantRepository != null)
10        {
11            var tenant = await tenantRepository.GetTenantByHost(host);
12
13            if (tenant != null)
14            {
15                context.TenantIdOrName = tenant.Name;
16            }
17        }
18    }
19
20    public override string Name => "Host";
21}

Configure the AbpTenantResolveOptions in the Web project at ConfigureServices method

1Configure<AbpTenantResolveOptions>(options =>
2{
3    options.TenantResolvers.Clear();
4    options.TenantResolvers.Add(new HostTenantResolveContributor());
5});

Update the CustomHostBrandingProvider in the Web project to display the current tenant name.

1[Dependency(ReplaceServices = true)]
2public class CustomHostBrandingProvider : DefaultBrandingProvider
3{
4    private readonly ICurrentTenant _currentTenant;
5    public override string AppName => _currentTenant.Name ?? "CustomHost";
6
7    public CustomHostBrandingProvider(ICurrentTenant currentTenant)
8    {
9        _currentTenant = currentTenant;
10    }
11}

This will display the current tenant name in the home page.

Now create the new tenant and set the host name and use that host name to login to the app.

GitHub Repo : https://github.com/antosubash/TenantWithCustomHost