Modular, Multi-tenant, ASP.NET Core Applications with Dotnettency - Part 4

In previous tutorials we have seen how to configure an core application for multiple tenants, which included setting up the Moogle and Gicrosoft tenants. In this tutorial, we will allow each tenant to have it’s own middleware pipeline. This allows us to precisely control which middlware runs for which tenants! This feature is sometimes referred to as Per Tenant Middleware in ASP.NET Core.

This is part of a series. You can find the other parts here You can find the sample code for this post here

Per Tenant Middleware you say?

Suppose you have some middleware, like:

  • UseWelcomePage
  • UseMvc
  • UseBlah

Great! But your application is also used by multiple Tenants. You would like to enable the welcome page middleware for one of those tenants but not for the others! Let’s see how the Dotnettency.MiddlewarePipeline nuget package is your friend!

Welcome Moogle!

We are goiing to call the UseWelcomePage() on an IApplicationBuilder to add the WelcomePage middleware - nothing unusual here. The only difference is, the IApplicationBuilder we call this on will be for the Moogle Tenant. The Gicrosoft tenant will remain unaffected.

  1. Ensure you have a Package Reference to Dotnettency.MiddlewarePipeline nuget package,
  2. In startup.cs in ConfigureServices() - find your call to AddMultiTenancy() as per the below:

     1        // This method gets called by the runtime. Use this method to add services to the container.
     2        // For more information on how to configure your application, visit
     3        public IServiceProvider ConfigureServices(IServiceCollection services)
     4        {
     5            return services.AddMultiTenancy<Tenant>((multiTenancyOptions) =>
     6            {
     7                multiTenancyOptions
     8                    .InitialiseTenant<TenantShellFactory>()
     9                    .ConfigureTenantContainers((containerBuilder)=> {
    10                        containerBuilder.WithStructureMap((tenant, tenantServices) =>
    11                        {
    13                        });
    14                    })
    15                    .ConfigureTenantMiddleware((tenantsMiddlewareBuilder) =>
    16                    {
    17                        tenantsMiddlewareBuilder.OnInitialiseTenantPipeline((context, appBuilder) =>
    18                        {
    20                        });
    21                    });
    22            });
    23        }

  3. Inside ConfigureTenantMiddleware is where we can add any tenant specific middleware. If the above seems unfamiliar to you, please refer to the previous tutorials. Let’s add the WelcomePage middleware for the Moogle tenant:

     1                    .ConfigureTenantMiddleware((tenantsMiddlewareBuilder) =>
     2                    {
     3                        tenantsMiddlewareBuilder.OnInitialiseTenantPipeline((context, appBuilder) =>
     4                        {
     5                            if (context.Tenant.Name == "Moogle")
     6                            {
     7                                appBuilder.UseWelcomePage();
     8                            }
     9                        });
    10                    });                            

I won’t cover the TenantShellFactory in this tutorial as it has already been covered previously. Suffice it to say that this class is responsible for identifying who the current Tenant is, based on the URL or any other accessible value. In our sample we have mapped localhost:5000 to the Moogle Tenant and localhost:5002 to Gicrosoft Tenant.

If we run the application, and browse on localhost:5000 (i.e Moogle) we get:


Success! That’s a beautiful looking welcome page. Very blue.

If we browse our application on localhost:5002 we get:


Hurray! This means our middleware is only running for the Tenant we wanted it to run for!

Furthermore, this works with any existing core Middleware today. The middleware does not have to be written with multitenancy in mind in any way.

What’s next?

Have any multitenant scenarios you want me to cover for core? Let me know! Stay tuned for:

  1. Per Tenant IHostingEnvironment (each tenant has its own virtual file system)
  2. Modules (Shared and Routed)

Don’t forget to leave a comment if you find this stuff useful!

This is part of a series. You can find the other parts here You can find the sample code for this post here

comments powered by Disqus