Why Generative AI Chatbots Are Becoming Business Essentials
Did you know that 73% of enterprises plan to double their AI‑driven customer interactions by 2025? Companies are turning to large‑language models to deliver instant, context‑aware answers, and developers are looking for the quickest path from idea to production. .NET 8 Minimal APIs paired with Azure OpenAI give you exactly that: a lightweight, high‑performance backend that can call state‑of‑the‑art generative models without managing complex infrastructure.
System Requirements and Project Bootstrap
Before writing any code, confirm that your workstation runs Windows 10/11 (or a recent Linux distro) with the .NET 8 SDK installed. Azure CLI version 2.45 or newer is required to provision resources. The following commands set up the environment:
dotnet --version # should show 8.0.x
az --version # confirm 2.45+
mkdir GenChatbot && cd GenChatbot
dotnet new web -n ChatApi --framework net8.0After the template is created, add the Azure.AI.OpenAI NuGet package, which provides a thin wrapper around the Azure OpenAI REST API.
dotnet add package Azure.AI.OpenAIThis prepares the solution for the minimal‑API style introduced in .NET 6 and refined in .NET 8.
Defining a Minimal API Endpoint in C#
Minimal APIs let you declare routes directly in Program.cs, eliminating controllers and boilerplate. The following snippet creates a POST /chat endpoint that accepts a JSON payload with a message field.
using Azure.AI.OpenAI;
using Azure;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(provider =>
{
var endpoint = new Uri(Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")!);
var credential = new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY")!);
return new OpenAIClient(endpoint, credential);
});
var app = builder.Build();
app.MapPost("/chat", async (ChatRequest req, OpenAIClient client) =>
{
var deployment = "gpt-4o-mini"; // Azure OpenAI deployment name
var options = new ChatCompletionsOptions();
options.Messages.Add(new ChatMessage(ChatRole.System, "You are a helpful assistant."));
options.Messages.Add(new ChatMessage(ChatRole.User, req.Message));
var response = await client.GetChatCompletionsAsync(deployment, options);
var reply = response.Value.Choices[0].Message.Content;
return Results.Json(new { reply });
});
app.Run();
public record ChatRequest(string Message);
Notice the use of OpenAIClient as a singleton, which reuses the underlying HTTP connection pool for optimal throughput.
Configuring Azure OpenAI Resources
Log in to the Azure portal and create an Azure OpenAI resource in the East US region. After provisioning, record the endpoint URL (e.g., https://myopenai.openai.azure.com/) and the primary key. Then, create a deployment named gpt-4o-mini using the model version released in March 2024. Store these values in a .env file or Azure App Service application settings:
AZURE_OPENAI_ENDPOINT=https://myopenai.openai.azure.com/
AZURE_OPENAI_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxWhen the application starts, the environment variables are read automatically by the DI container defined earlier.
Local Testing with curl and Swagger
Run the API with dotnet run. The minimal API template includes Swagger UI at http://localhost:5080/swagger, allowing you to experiment without writing a client. For quick command‑line verification, use curl:
curl -X POST http://localhost:5080/chat \
-H "Content-Type: application/json" \
-d '{"Message":"What are the benefits of using Minimal APIs?"}'The response will contain a generated answer, confirming that the Azure OpenAI service is correctly invoked.
Deploying to Azure App Service
Once the endpoint works locally, push the code to GitHub and create an Azure App Service using the dotnet runtime. The deployment pipeline can be set up with GitHub Actions:
name: Deploy to Azure
on:
push:
branches: [ main ]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
- name: Publish
run: dotnet publish -c Release -o ./publish
- name: Deploy to Azure Web App
uses: azure/webapps-deploy@v2
with:
app-name: my-gen-chatbot
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: ./publishRemember to add the same AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_KEY as application settings in the App Service configuration blade. After deployment, the endpoint is reachable at https://my-gen-chatbot.azurewebsites.net/chat.
Performance Tips and Cost Management
Azure OpenAI pricing is token‑based. To keep costs under control, limit the max_tokens parameter in ChatCompletionsOptions to 150 for typical Q&A scenarios. Enable response caching with app.UseResponseCaching() and add Cache-Control headers for identical prompts received within a short window. Monitoring can be done via Azure Monitor metrics, which expose request latency, token usage, and error rates.
Conclusion
By leveraging .NET 8 Minimal APIs, you can spin up a production‑grade generative AI chatbot in under an hour, while Azure OpenAI handles the heavy lifting of language understanding. The combination delivers low latency, native C# development experience, and seamless scaling on Azure App Service. Start experimenting today, tune your prompts, and watch your conversational UI evolve without rewriting the backend.
Sources
Microsoft Docs – Azure OpenAI Service
Microsoft Docs – .NET 8 Minimal APIs
Azure Architecture Center – Building AI‑enabled web apps
Author: Mahmut Sarıkaya — sarikayadev.com