Ir al contenido
Docs Try Aspire
Docs Try

Connect to Azure AI Inference

Esta página aún no está disponible en tu idioma.

Azure AI Inference logo 🧪 Preview

This page describes how consuming apps connect to an Azure AI Inference endpoint that’s already registered as a connection string in your AppHost. For instructions on registering the connection, see Get started with the Azure AI Inference integrations.

When you reference an Azure AI Inference connection from your AppHost, Aspire injects the connection string into the consuming app as an environment variable. Your app can either read the environment variable directly — the pattern works from any language — or, in C#, use the Aspire.Azure.AI.Inference client integration for automatic dependency injection, health checks, and telemetry.

Aspire injects the full connection string into consuming apps:

  • .NET apps: available via IConfiguration under the key ConnectionStrings:{resourcename}
  • All other apps: available as the environment variable ConnectionStrings__{resourcename} (note the double underscore)

Connection string format:

Endpoint=https://{endpoint}/;Key={apikey};DeploymentId={deploymentName}
ComponentDescription
EndpointThe Azure AI Inference service endpoint URL
KeyThe API key for authentication
DeploymentIdThe model deployment name

Example:

Endpoint=https://myresource.services.ai.azure.com/models;Key=abc123;DeploymentId=gpt-4o-mini

Pick the language your consuming app is written in. Each example assumes your AppHost registers a connection string named ai-foundry and references it from the consuming app.

For C# apps, the recommended approach is the Aspire Azure AI Inference client integration. It registers a ChatCompletionsClient through dependency injection and adds health checks and telemetry automatically. If you’d rather read the connection string directly, see the Read the connection string section at the end of this tab.

Install the 📦 Aspire.Azure.AI.Inference NuGet package in the client-consuming project:

.NET CLI — Add Aspire.Azure.AI.Inference package
dotnet add package Aspire.Azure.AI.Inference

In Program.cs, call AddAzureChatCompletionsClient on your IHostApplicationBuilder to register a ChatCompletionsClient:

C# — Program.cs
builder.AddAzureChatCompletionsClient(connectionName: "ai-foundry");

Resolve the client through dependency injection:

C# — ExampleService.cs
public class ExampleService(ChatCompletionsClient client)
{
// Use client...
}

Chain AddChatClient to also register an IChatClient from Microsoft.Extensions.AI:

C# — Program.cs
builder.AddAzureChatCompletionsClient(connectionName: "ai-foundry")
.AddChatClient("gpt-4o-mini");

Resolve the IChatClient through dependency injection:

C# — ExampleService.cs
public class ExampleService(IChatClient chatClient)
{
public async Task<string> GetResponseAsync(string userMessage)
{
var response = await chatClient.CompleteAsync(userMessage);
return response.Message.Text ?? string.Empty;
}
}

Call AddAzureEmbeddingsClient to register an EmbeddingsClient:

C# — Program.cs
builder.AddAzureEmbeddingsClient(connectionName: "ai-foundry");

To register multiple clients with different connection names, use the keyed variants:

C# — Program.cs
builder.AddKeyedAzureChatCompletionsClient(name: "chat");
builder.AddKeyedAzureChatCompletionsClient(name: "code");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("chat")] ChatCompletionsClient chatClient,
[FromKeyedServices("code")] ChatCompletionsClient codeClient)
{
// Use clients...
}

For more information, see Keyed services in .NET.

The Aspire Azure AI Inference client integration offers multiple ways to provide configuration.

Connection strings. The integration reads the connection string from the ConnectionStrings configuration section automatically when you pass the connection name to AddAzureChatCompletionsClient:

JSON — appsettings.json
{
"ConnectionStrings": {
"ai-foundry": "Endpoint=https://{endpoint}/;Key={apikey};DeploymentId={deploymentName}"
}
}

Configuration providers. The integration supports Microsoft.Extensions.Configuration. It loads ChatCompletionsClientSettings from configuration using the Aspire:Azure:AI:Inference key:

JSON — appsettings.json
{
"Aspire": {
"Azure": {
"AI": {
"Inference": {
"DisableTracing": false,
"EnableSensitiveTelemetryData": false
}
}
}
}
}

Inline delegates. Pass an Action<ChatCompletionsClientSettings> to configure settings inline:

C# — Program.cs
builder.AddAzureChatCompletionsClient(
connectionName: "ai-foundry",
configureSettings: static settings => settings.DisableTracing = true);

The Azure AI Inference client integration participates in Aspire health checks. The integration wires into the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

The Aspire Azure AI Inference client integration automatically configures logging, tracing, and metrics through OpenTelemetry.

Logging categories:

  • Azure.Core
  • Azure.Identity

Tracing activities:

  • Experimental.Microsoft.Extensions.AI

If you prefer not to use the Aspire client integration, install the 📦 Azure.AI.Inference NuGet package and read the connection string from IConfiguration directly:

C# — Program.cs
using Azure;
using Azure.AI.Inference;
var connectionString = builder.Configuration.GetConnectionString("ai-foundry")!;
var parts = connectionString.Split(';')
.Select(p => p.Split('=', 2))
.Where(p => p.Length == 2)
.ToDictionary(p => p[0].Trim(), p => p[1].Trim());
var endpoint = new Uri(parts["Endpoint"]);
var apiKey = parts["Key"];
var deploymentId = parts["DeploymentId"];
var client = new ChatCompletionsClient(endpoint, new AzureKeyCredential(apiKey));