Why Azure OpenAI matters for .NET developers
Did you know that more than 60% of enterprise AI projects now rely on cloud‑native models? Azure OpenAI gives C# teams instant access to GPT‑4‑level capabilities without managing GPUs, while .NET 8 brings performance boosts that make real‑time generation feasible. The combination eliminates the traditional gap between data scientists and application engineers.
System requirements and Azure resource provisioning
Before writing code, confirm you have .NET 8 SDK (released November 2023) and an Azure subscription with the OpenAI resource enabled. In the Azure portal, create an "Azure OpenAI" resource, select the desired model (e.g., gpt‑35‑turbo), and note the endpoint URL and access key. The portal also shows a usage quota of 1 M tokens per month for the free tier, enough for early prototypes.
Creating a .NET 8 console project
Open a terminal and run the following commands. The first line checks the SDK version; the second creates the project; the third adds the official client library.
dotnet --version
# Expected output: 8.0.x
dotnet new console -n AzureOpenAIDemo
cd AzureOpenAIDemo
dotnet add package Azure.AI.OpenAI --prereleaseAfter the restore finishes, open Program.cs and prepare to inject the client.
Calling Azure OpenAI from C#
The Azure.AI.OpenAI SDK mirrors the REST API but handles authentication and pagination for you. Below is a minimal async method that sends a prompt and prints the first choice.
using Azure.AI.OpenAI;
using Azure;
var endpoint = new Uri("https://my-resource.openai.azure.com/");
var credential = new AzureKeyCredential("YOUR_KEY");
var client = new OpenAIClient(endpoint, credential);
var options = new CompletionsOptions()
{
Prompt = "Explain the difference between async and await in C# in two sentences.",
MaxTokens = 60,
Temperature = 0.7f
};
Response<Completions> response = await client.GetCompletionsAsync("gpt-35-turbo", options);
Console.WriteLine(response.Value.Choices[0].Text);
Notice the use of CompletionsOptions to control token limits and temperature. Adjust MaxTokens based on cost; each token roughly equals 4 characters, and Azure charges per 1 000 tokens.
Deploying the logic as an Azure Function
Serverless execution lets you scale generation on demand and keep secrets out of the code base. Create a new isolated .NET 8 function app with the Azure Functions Core Tools.
dotnet new func -n OpenAIFunctionApp --worker-runtime dotnetIsolated --target-framework net8.0
cd OpenAIFunctionApp
dotnet add package Azure.AI.OpenAI
dotnet add package Microsoft.Azure.Functions.Worker.Sdk
dotnet add package Microsoft.Azure.Functions.Worker.Extensions.HttpReplace the autogenerated Function1.cs with the following trigger. The function reads a JSON body containing prompt, forwards it to Azure OpenAI, and returns the generated text.
using System.Net;
using Azure.AI.OpenAI;
using Azure;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
public class GenerateTextFunction
{
private readonly OpenAIClient _client;
private readonly ILogger _logger;
public GenerateTextFunction(ILoggerFactory loggerFactory)
{
var endpoint = new Uri(Environment.GetEnvironmentVariable("OPENAI_ENDPOINT"));
var credential = new AzureKeyCredential(Environment.GetEnvironmentVariable("OPENAI_KEY"));
_client = new OpenAIClient(endpoint, credential);
_logger = loggerFactory.CreateLogger();
}
[Function("GenerateText")]
public async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req)
{
var request = await req.ReadFromJsonAsync();
var options = new CompletionsOptions { Prompt = request.Prompt, MaxTokens = 150 };
var response = await _client.GetCompletionsAsync("gpt-35-turbo", options);
var result = response.Value.Choices[0].Text;
var httpResponse = req.CreateResponse(HttpStatusCode.OK);
await httpResponse.WriteStringAsync(result);
return httpResponse;
}
}
public record PromptRequest(string Prompt);
Deploy with func azure functionapp publish MyFunctionApp. Azure automatically injects the environment variables you define in the Function App settings, keeping keys out of source control.
Performance tuning and cost control
.NET 8 introduces native AOT compilation, which can shrink the function cold‑start time to under 200 ms. Add the PublishAot=true flag in the project file and rebuild. On the AI side, use logprobs only when you need token‑level confidence; otherwise disable it to save tokens. Monitoring can be achieved with Azure Monitor metrics for "Tokens Used" and Function App latency.
Real‑world example: summarizing customer feedback
Imagine a retail dashboard that ingests 10 000 feedback entries nightly. A .NET 8 background service batches 200‑record chunks, calls the function endpoint, and stores the summary in Azure Cosmos DB. The entire pipeline processes 10 000 records in roughly 3 minutes, well within a typical SLA, while staying under $5 of daily AI spend.
Conclusion
Integrating Azure OpenAI with .NET 8 gives developers a production‑ready stack for generative AI: modern language features, high‑performance runtimes, and seamless serverless deployment. By following the steps above—provisioning the resource, wiring the SDK, and exposing the logic through Azure Functions—you can move from prototype to scalable service in a single day.
Sources
Microsoft Azure OpenAI documentation, Azure Functions .NET isolated worker guide, .NET 8 release notes.
Author: Mahmut Sarıkaya — sarikayadev.com