The .NET SDKs provide comprehensive access to Azure AI services including Foundry Local, Azure Machine Learning, and Azure AI Search. This guide covers installation, authentication, and usage examples for C#.
using Azure.AI.Projects.OpenAI;using Azure.Identity;string endpoint = "https://<resource-name>.services.ai.azure.com/api/projects/<project-name>";AIProjectClient projectClient = new( endpoint: new Uri(endpoint), tokenProvider: new DefaultAzureCredential());
using OpenAI.Responses;#pragma warning disable OPENAI001OpenAIResponseClient responseClient = projectClient.OpenAI.GetProjectResponsesClientForModel("gpt-5.2");OpenAIResponse response = responseClient.CreateResponse("What is the speed of light?");Console.WriteLine(response.GetOutputText());#pragma warning restore OPENAI001
using Microsoft.AI.Foundry.Local;// Load modelvar model = await catalog.GetModelAsync(alias: "qwen2.5-0.5b");await model.LoadAsync();// Get model path for inferencevar modelPath = await model.GetPathAsync();// Use model with native API (implementation varies by model type)Console.WriteLine($"Model ready at: {modelPath}");
// Start REST API serverawait manager.StartWebServerAsync();Console.WriteLine($"Server running at: {config.Web.Urls}");// Stop server when doneawait manager.StopWebServerAsync();
using Azure.Search.Documents;using Azure.Identity;using System;Uri endpoint = new Uri("https://<search-service>.search.windows.net");string indexName = "your-index";SearchClient searchClient = new SearchClient( endpoint, indexName, new DefaultAzureCredential());
using Azure.Search.Documents.Models;using System.Collections.Generic;var documents = new[]{ new SearchDocument { ["id"] = "doc1", ["page_chunk"] = "Phoenix is a major city in Arizona.", ["page_number"] = 104 }, new SearchDocument { ["id"] = "doc2", ["page_chunk"] = "The Phoenix metropolitan area includes Glendale.", ["page_number"] = 105 }};IndexDocumentsResult result = await searchClient.IndexDocumentsAsync( IndexDocumentsBatch.Upload(documents));Console.WriteLine($"Uploaded {result.Results.Count} documents");
using Azure.AI.ContentSafety;using Azure.Identity;Uri endpoint = new Uri("https://<resource>.cognitiveservices.azure.com");ContentSafetyClient client = new ContentSafetyClient( endpoint, new DefaultAzureCredential());var request = new AnalyzeTextOptions("Sample text to analyze");var response = await client.AnalyzeTextAsync(request);Console.WriteLine($"Hate: {response.Value.HateResult.Severity}");Console.WriteLine($"Violence: {response.Value.ViolenceResult.Severity}");