- SajanTech's
- Posts
- Using Cloudflare DNS Resolver with ABP Framework
Using Cloudflare DNS Resolver with ABP Framework
Enhancing multi-tenant SaaS apps with secure and reliable DNS lookups
When building modern applications on top of the ABP Framework, developers often need to handle domain name resolution. For example, validating custom domains, configuring multi-tenant SaaS apps, or integrating with external APIs that require DNS checks.
Instead of relying on system-level DNS, you can integrate with Cloudflare’s DNS resolver to achieve fast, secure, and privacy-first resolution. In this post, we’ll explore how to integrate Cloudflare’s DNS resolver into an ABP Framework application.
Prerequisite
Before diving in, you should be familiar with the ABP Framework basics, including:
Creating and working with ABP modules.
Using dependency injection (
ITransientDependency
,IApplicationService
).Building and exposing application services via APIs.
If you’ve already built a simple ABP application, this guide will feel natural.
Why Use Cloudflare as a DNS Resolver?
Cloudflare offers a public DNS resolver (1.1.1.1 and 1.0.0.1) that is:
⚡ Fast – One of the fastest DNS resolvers globally.
🔒 Secure – Built-in DNS-over-HTTPS (DoH) and DNS-over-TLS support.
🛡️ Privacy-first – Cloudflare commits to never logging personal data.
For an ABP-based SaaS platform, you might use Cloudflare DNS for:
Custom domain validation (when tenants bring their own domain).
Health checks against external services.
Failover logic with multiple service endpoints.
Setting Up a DNS Resolver Service in ABP
ABP Framework uses modularity and dependency injection (DI). We can create a reusable DNS Resolver Module that integrates Cloudflare’s resolver.
Step 1: Install a DNS Client Library
We’ll use DnsClient.NET
which supports custom DNS servers.
dotnet add package DnsClient
Step 2: Create a DNS Resolver Service
using DnsClient;
using System.Net;
using System.Threading.Tasks;
namespace MyCompany.MyProject.Dns
{
public class CloudflareDnsResolver : ITransientDependency
{
private readonly LookupClient _client;
public CloudflareDnsResolver()
{
// Configure Cloudflare DNS (1.1.1.1 and 1.0.0.1)
_client = new LookupClient(
new IPEndPoint(IPAddress.Parse("1.1.1.1"), 53),
new IPEndPoint(IPAddress.Parse("1.0.0.1"), 53))
{
UseCache = true,
Timeout = TimeSpan.FromSeconds(5)
};
}
public async Task<string?> ResolveAsync(string domain)
{
var result = await _client.QueryAsync(domain, QueryType.A);
var record = result.Answers.ARecords().FirstOrDefault();
return record?.Address.ToString();
}
}
}
Here we defined a simple service that queries Cloudflare’s DNS for an A
record.
Step 3: Use It Inside an ABP Application Service
In ABP, you typically interact with domain logic via application services. Let’s inject the resolver:
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
namespace MyCompany.MyProject.Domains
{
public class DomainAppService : ApplicationService
{
private readonly CloudflareDnsResolver _dnsResolver;
public DomainAppService(CloudflareDnsResolver dnsResolver)
{
_dnsResolver = dnsResolver;
}
public async Task<string?> CheckDomainAsync(string domain)
{
return await _dnsResolver.ResolveAsync(domain);
}
}
}
This can be exposed via an API endpoint for tenant domain validation.
Step 4: Register as a Module
Create a simple module to register the service:
using Volo.Abp.Modularity;
namespace MyCompany.MyProject.Dns
{
public class DnsResolverModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddTransient<CloudflareDnsResolver>();
}
}
}
Now you can add it to your MyProjectHttpApiHostModule
or ApplicationModule
.
Example Use Case: Multi-Tenant SaaS Domain Validation
If your ABP-based SaaS allows customers to use custom domains, you can use the resolver to:
Verify the domain exists.
Check if the domain points to your SaaS platform’s IP.
Automatically validate DNS propagation.
public async Task<bool> ValidateTenantDomainAsync(string domain)
{
var ip = await _dnsResolver.ResolveAsync(domain);
return ip == "203.0.113.42"; // your SaaS app server IP
}
Conclusion
By integrating Cloudflare DNS resolver into your ABP Framework project, you can build reliable domain-related features like tenant domain verification, external service health checks, and failover routing.
ABP’s modular architecture makes it easy to package this as a reusable DNS Module, ensuring consistent and secure DNS lookups across your application.
Reply