Multi-Tenancy Support in Angular Apps with ABP.IO

In today's rapidly evolving digital landscape, implementing multi-tenant architecture has become essential for web applications to ensure scalability, efficiency, and cost-effectiveness. One of the simplest and most effective methods is tenant identification using subdomains. In this post, we explore how to implement multi-tenancy in Angular applications using the ABP.IO framework and the URL structure: https://{tenant}.mydomain.com/.

What is Multi-Tenancy and Why Is It Important?

Multi-tenancy allows a single instance of an application to serve multiple clients (tenants), with each tenant’s data and configurations remaining isolated. This architecture simplifies deployment, updates, and maintenance, reducing both operational costs and complexity. It’s a key factor in the success of modern web applications that need to scale efficiently.

ABP.IO and Multi-Tenancy Support

ABP.IO is a robust framework designed for enterprise application development using .NET and Angular. It provides comprehensive multi-tenancy support out of the box, offering various strategies for tenant identification such as subdomains, HTTP headers, and request parameters.

Using Subdomains to Identify the Tenant

The SubdomainTenantResolveContributor in ABP.IO enables tenant identification via the URL’s subdomain. For instance, in https://tenant1.mydomain.com/, the tenant is identified as tenant1.

Backend Configuration

To enable subdomain-based tenant resolution on the backend, you need to configure the tenant resolver:

csharp

Copy code

// In YourProjectNameHttpApiHostModule.cs or Startup.cs

Configure<AbpTenantResolveOptions>(options => {
    options.TenantResolvers.Clear();
    options.TenantResolvers.Add(new SubdomainTenantResolveContributor());
});

This ensures that the backend automatically detects the tenant from the subdomain used in the URL.

Frontend Configuration

On the frontend, ABP.IO’s multi-tenancy functionality is already built-in. Since tenant identification is handled by the backend, frontend developers don’t need to explicitly add tenant information to request headers or parameters. However, there are a few key considerations to keep in mind:

Get Tenant Information Using ConfigStateService

Angular’s ConfigStateService can be used to access configuration data transferred from the backend, which includes the current tenant ID.

typescript

Copy code

const tenant = inject(ConfigStateService).getDeep("currentUser.tenantId");

Setting the Base URL

Ensure your Angular app is configured to handle subdomains correctly. If you are deploying separate instances for each tenant, set the base href in index.html as follows:

html

Copy code

<base href="/">

Handling Redirects

Since the backend handles tenant identification through subdomains, it’s important to ensure that all redirects and authentication flows respect the current subdomain. For instance, when redirecting users to the login page, use relative paths to maintain the subdomain context:

typescript

Copy code

this.router.navigate(['/account/login']);

Conclusion

Leveraging subdomains for multi-tenancy in Angular applications with ABP.IO is a streamlined and powerful approach. Most of the functionality is already built into the framework, meaning frontend developers can implement multi-tenancy without extensive code changes. By following these straightforward guidelines, you can quickly configure your application to support a multi-tenant architecture.


Ready to streamline your Angular app with multi-tenancy support? Contact us today to see how our expertise with ABP.IO can help you build scalable, efficient web applications tailored to your business needs. Let’s discuss how we can partner to elevate your application development!

Contact us.

If you need a partner in software development, we're here to help you.

We will respond to your enquiry immediately.