Introduction
Modern .NET applications rarely rely on a single AI provider. Organizations may use Azure OpenAI for enterprise compliance, OpenAI for advanced reasoning, local models for sensitive workloads, or other providers to optimize cost and availability. Hardcoding a single provider into your application makes it difficult to adapt when requirements change or a provider becomes unavailable.
The Microsoft.Extensions.AI library provides a common abstraction for integrating AI capabilities into .NET applications. By programming against shared interfaces instead of provider-specific SDKs, you can build applications that support provider switching, failover, testing, and future extensibility with minimal code changes.
In this article, you'll learn production-ready patterns for implementing provider switching using Microsoft.Extensions.AI, along with best practices, common pitfalls, and architectural guidance.
What Is Microsoft.Extensions.AI?
Microsoft.Extensions.AI is a .NET library that provides common abstractions for AI services. Rather than coupling your application to a specific vendor, you code against interfaces that multiple providers can implement.
Some benefits include:
Provider-independent application code
Easier testing with mock implementations
Simplified migration between AI providers
Centralized dependency injection
Cleaner architecture
Instead of referencing provider SDKs throughout your codebase, only the infrastructure layer needs provider-specific implementations.
Why Provider Switching Matters
Provider switching is valuable in several production scenarios:
| Scenario | Benefit |
|---|---|
| Provider outage | Automatically fail over to another provider |
| Cost optimization | Route requests to lower-cost models when appropriate |
| Compliance requirements | Use approved providers for regulated workloads |
| Performance optimization | Choose providers based on latency or workload |
| Development and testing | Replace cloud models with local or mock implementations |
Without abstraction, switching providers often requires changes throughout the application.
Architecture Overview
A typical architecture looks like this:
Application
│
Business Services
│
IChatClient
│
Provider Router
│
┌────────────┬───────────────┬─────────────┐
│ │ │
Azure AI OpenAI Local Model
Business services communicate only with the abstraction, not directly with provider SDKs.
Registering AI Services
Using dependency injection keeps your application loosely coupled.
builder.Services.AddSingleton<IChatClient>(serviceProvider =>
{
return CreateChatClient();
});
The registration method can determine which provider should be used based on configuration or runtime policies.
Using Configuration for Provider Selection
Store the active provider in configuration instead of hardcoding it.
appsettings.json
{
"AI": {
"Provider": "AzureOpenAI"
}
}
Configuration-driven provider selection allows changes without recompiling the application.
Creating a Provider Factory
A factory centralizes provider creation.
public interface IChatClientFactory
{
IChatClient Create(string providerName);
}
Implementation details remain isolated from the rest of the application.
Runtime Provider Switching
Applications may need different providers for different workloads.
For example:
| Request Type | Preferred Provider |
|---|---|
| Customer Support | Azure OpenAI |
| Internal Assistant | Local Model |
| Code Generation | OpenAI |
| Document Analysis | Azure OpenAI |
Instead of a single provider, a routing service can evaluate request metadata and choose the appropriate implementation.
Implementing a Router
public interface IAiRouter
{
IChatClient GetClient(string workload);
}
The router becomes responsible for selecting the correct provider.
Business services remain unaware of vendor-specific logic.
Implementing Failover
Production systems should anticipate temporary provider failures.
A common strategy is:
Join the conversation! Your thoughts help the community grow.