Building a ServiceClient Factory for Multi-Tenant Dynamics 365 / Dataverse Apps
Working with more than one Power Platform or Dynamics 365 environment can quickly become a headache—especially when you’re building a SaaS solution that needs to connect to many different customer instances and it needs to run in Azure. In my case, I have a multi-tenant app that uses the same client credentials to connect to different customer Dataverse environments. The only thing that differs between them is the instance URL. How to manage all the Dataverse connections? The multiple instances of ServiceClient / OrganizationService in .NET? At first, this setup sounds manageable. But in practice, managing connection lifetimes, reusing them efficiently, and avoiding unnecessary re-authentication is more difficult than it looks. Scoped vs Singleton My first idea was to avoid a Singleton and instead create a new ServiceClient on every request (scoped). Just reuse the credentials and pass different URLs into the ServiceClient, right? The problem is that creating a new ServiceClient can be slow (often >300ms) due to the authentication process. Each scoped instance also consumes a connection from the connection pool. This becomes especially problematic in consumption-based Azure Functions, where you’re limited to 600 outbound connections. Each new ServiceClient reserves a connection, and it can take up to … Continue reading Building a ServiceClient Factory for Multi-Tenant Dynamics 365 / Dataverse Apps
Copy and paste this URL into your WordPress site to embed
Copy and paste this code into your site to embed