Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Tenant-based dependency injection in multi-tenant ASP.NET Core applications

One need in multitenant applications is injecting dependencies based on tenant configuration. It can be actually more complex as instances may need constructor parameters. Here is my example of dynamic injection of multiple file clients in ASP.NET Core multi-tenant web application.

Source code available! Source code for all my ASP.NET Core multi-tenant application posts is available at public Github repository gpeipman/AspNetCoreMultitenant.

The case of file client

We are living in cloud era. It means that customers of our multitenant application may use different cloud services for their files. Of course, some customers want us to take care of everything. So, how to handle this situation? One customers wants files to be stored on Azure Blob Storage and another wants to go with Google Drive.

We can go with IFileClient generalization I have introduced in my previous posts.

NB! To keep things mininal I’m using dummy IFileClient and implementing classes in this blog post. The main focus here is on tenant based Dependency Injection.

Dummy file clients

Here are the dummy file clients I’m using for this post.

public interface IFileClient
{
}

public class AzureBlobStorageFileClient : IFileClient
{
    public AzureBlobStorageFileClient(string connectionString)
    {
    }
}

public class GoogleDriveFileClient : IFileClient
{
    public GoogleDriveFileClient(string connectionString)
    {
    }
}

They don’t have any members besides constructor that takes storage connection string as an argument.

Configuring tenants

I will go with JSON-file tenants provider from my blog post Implementing tenant providers on ASP.NET Core. Notice that all tenants have storage type and storage connection string specified in their configuration.

[
  {
    "Id": 1,
    "Host": "sme1:5000",
    "DatabaseType": 1,
    "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=MultitenantWeb_SME;Trusted_Connection=True;MultipleActiveResultSets=true",
    "Name": "Small biz 1",
    "StorageType": "GoogleDrive",
    "StorageConnectionString"""
  },
  {
    "Id": 2,
    "Host": "sme2:5000",
    "DatabaseType": 1,
    "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=MultitenantWeb_SME;Trusted_Connection=True;MultipleActiveResultSets=true",
    "Name": "Small biz 2",
    "StorageType": "AzureBlob",
    "StorageConnectionString"""
  },
  {
    "Id": 3,
    "Host": "bigcorp:5000",
    "DatabaseType": 2,
    "ConnectionString": "Server=localhost;Database=multitenant;Uid=demo;Pwd=demo",
    "Name": "Big corp",
    "StorageType": "AzureBlob",
    "StorageConnectionString": ""
  }
]

Tenant 1 with host name sme1 uses Google Drive for files and tenants 3 with host name bigcorp uses Azure blob storage.

Tenant-based dependency injection

It’s time to head to application Startup class and make dependency injection of IFileClient consider also current tenant. I will use implementation factory function to return correct file client instance from dependency injection.

services.AddScopedIFileClient>(service =>
{
    var provider = service.GetRequiredServiceITenantProvider>();
    var tenant = provider.GetTenant();

    if(tenant.StorageType == "GoogleDrive")
    {
        return new GoogleDriveFileClient(tenant.ConnectionString);
    }

    if(tenant.StorageType == "AzureBlob")
    {
        return new AzureBlobStorageFileClient(tenant.ConnectionString);
    }

    return null;
});

For unknown and missing file clients my code returns null. It’s not perfect joice but works for this demo.

Trying out file client injection

To see if file clients are injected correctly I will try file client injection out with sme1 and bigcorp tenants.

Both tenants get the correct type of instance as expected.

From here

It’s possible to have more complex cases when dynamic dependency injection is needed in multi-tenant application. There are some interesting works to check out:

  • Multi-tenant Dependency Injection in ASP.NET Core by Ben Foster
  • Implement dynamic dependency injection in ASP.NET Core 2.2, example with multitenancy scenario by Anthony Giretti
  • Multitenant Applications (Autofac)

Wrapping up

Using my previous works on multi-tenant ASP.NET Core applications and IFileClient generalization we were able to define file client type and connection string in tenants configuration. Using implementation factory method and tenant provider we were able to inject correct instance of file client based on current tenant to ASP.NET Core controller.

The post Tenant-based dependency injection in multi-tenant ASP.NET Core applications appeared first on Gunnar Peipman - Programming Blog.



This post first appeared on Gunnar Peipman - Programming, please read the originial post: here

Share the post

Tenant-based dependency injection in multi-tenant ASP.NET Core applications

×

Subscribe to Gunnar Peipman - Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×