AI-Driven Development
AI-Driven Development in ASP.NET Core
– In modern enterprise applications, AI transforms static APIs into intelligent systems that analyze user feedback, generate personalized content, and automate decision-making. This guide builds a production-ready Feedback Analysis API that uses OpenAI’s GPT-4o-mini to categorize customer feedback, extract sentiment, and suggest actionable insights—solving real-world problems like manual review bottlenecks while ensuring scalability and security for enterprise deployments.
OpenAI, Microsoft.Extensions.Http, Microsoft.EntityFrameworkCore.SqliteRun these commands to scaffold the project:
dotnet new webapi -o AiFeedbackApi --use-program-main
cd AiFeedbackApi
dotnet add package OpenAI --prerelease
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Design
code . Add your OpenAI key to appsettings.json using User Secrets in development:
// appsettings.json
{
"AI": {
"OpenAI": {
"ApiKey": "your-api-key-here",
"Model": "gpt-4o-mini"
}
},
"ConnectionStrings": {
"Default": "Data Source=feedback.db"
}
} Define our feedback entity using modern C# 13 primary constructors:
// Models/FeedbackItem.cs
public class FeedbackItem(int id, string text, string category, double sentimentScore)
{
public int Id { get; } = id;
public required string Text { get; init; } = text;
public string Category { get; set; } = category;
public double SentimentScore { get; set; } = sentimentScore;
public FeedbackItem() : this(0, string.Empty, string.Empty, 0) { }
} Create a robust, typed AI service using the official OpenAI client and HttpClientFactory fallback:
// Services/IAiFeedbackAnalyzer.cs
public interface IAiFeedbackAnalyzer
{
Task<(string Category, double SentimentScore)> AnalyzeAsync(string feedbackText);
}
// Services/AiFeedbackAnalyzer.cs
using OpenAI.Chat;
using OpenAI;
public class AiFeedbackAnalyzer(OpenAIClient client, IConfiguration config) : IAiFeedbackAnalyzer
{
private readonly ChatClient _chatClient = client.GetChatClient(config["AI:OpenAI:Model"] ?? "gpt-4o-mini");
public async Task<(string Category, double SentimentScore)> AnalyzeAsync(string feedbackText)
{
var messages = new List
{
new SystemChatMessage("""
Analyze customer feedback and respond ONLY with JSON:
{"category": "positive|negative|neutral|suggestion|bug", "sentiment": 0.0-1.0}
Categories: positive, negative, neutral, suggestion, bug.
Sentiment: 1.0 = very positive, 0.0 = very negative.
"""),
new UserChatMessage(feedbackText)
};
var response = await _chatClient.CompleteChatAsync(messages);
var jsonResponse = response.Value.Content[0].Text;
// Parse structured JSON response safely
using var doc = JsonDocument.Parse(jsonResponse);
var category = doc.RootElement.GetProperty("category").GetString() ?? "neutral";
var sentiment = doc.RootElement.GetProperty("sentiment").GetDouble();
return (category, sentiment);
}
} Register services in Program.cs with minimal APIs:
// Program.cs
using Microsoft.EntityFrameworkCore;
using OpenAI;
var builder = WebApplication.CreateBuilder(args);
var apiKey = builder.Configuration["AI:OpenAI:ApiKey"]
?? throw new InvalidOperationException("OpenAI ApiKey is required");
builder.Services.AddOpenAIClient(apiKey);
builder.Services.AddScoped<IAiFeedbackAnalyzer, AiFeedbackAnalyzer>();
builder.Services.AddDbContext(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("Default")));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapFallback(() => Results.NotFound());
app.Run();
// AppDbContext.cs
public class AppDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet<FeedbackItem> FeedbackItems { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<FeedbackItem>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Category).HasMaxLength(50);
});
}
} Add intelligent endpoints that process feedback in real-time:
// Add to Program.cs after app.Build()
app.MapPost("/api/feedback/analyze", async (IAiFeedbackAnalyzer analyzer, [FromBody] string text) =>
{
var (category, sentiment) = await analyzer.AnalyzeAsync(text);
return Results.Ok(new { Category = category, SentimentScore = sentiment });
});
app.MapPost("/api/feedback", async (AppDbContext db, IAiFeedbackAnalyzer analyzer, [FromBody] string text) =>
{
var (category, sentiment) = await analyzer.AnalyzeAsync(text);
var feedback = new FeedbackItem(0, text, category, sentiment);
db.FeedbackItems.Add(feedback);
await db.SaveChangesAsync();
return Results.Created($"/api/feedback/{feedback.Id}", feedback);
});
app.MapGet("/api/feedback/stats", async (AppDbContext db) =>
Results.Ok(await db.FeedbackItems
.GroupBy(f => f.Category)
.Select(g => new { Category = g.Key, Count = g.Count(), AvgSentiment = g.Average(f => f.SentimentScore) })
.ToListAsync())); Run dotnet run and test with Swagger or curl:
curl -X POST "https://localhost:5001/api/feedback/analyze" \
-H "Content-Type: application/json" \
-d '"The UI is intuitive and fast!"'
# Response: {"category":"positive","sentimentScore":0.92} Here’s our complete, optimized controller alternative using primary constructors and source generators:
[ApiController]
[Route("api/v1/[controller]")]
public class FeedbackController(AppDbContext db, IAiFeedbackAnalyzer analyzer) : ControllerBase
{
[HttpPost]
public async Task<IActionResult> AnalyzeAndStore([FromBody] AnalyzeRequest request)
{
ArgumentNullException.ThrowIfNull(request.Text);
var (category, sentiment) = await analyzer.AnalyzeAsync(request.Text);
var item = new FeedbackItem(0, request.Text, category, sentiment);
db.FeedbackItems.Add(item);
await db.SaveChangesAsync();
return CreatedAtAction(nameof(GetById), new { id = item.Id }, item);
}
[HttpGet("{id:int}")]
public async Task<IActionResult> GetById(int id) =>
await db.FeedbackItems.FindAsync(id) is { } item
? Ok(item)
: NotFound();
}
public record AnalyzeRequest(string Text); dotnet user-secrets and Azure Key Vault in prod.Polly retry policies: AddHttpClient().AddPolicyHandler(...).JsonDocument and provide fallbacks.IHostedService.text[..Math.Min(4000, text.Length)].IMemoryCache (TTL: 5min).IBackgroundService + Channels for batch analysis.IAiProvider to switch between OpenAI, Azure OpenAI, or local models.OpenAIClient with Moq.chatClient.CompleteChatStreamingAsync().You’ve built a production-grade AI-powered Feedback API that scales from MVP to enterprise. Next steps: integrate with Blazor frontend, add RAG with vector databases, or deploy to Azure Container Apps with auto-scaling. Keep iterating—AI development is about continuous improvement.
Implement exponential backoff with Polly:
services.AddHttpClient<IAiFeedbackAnalyzer>().AddPolicyHandler(
Policy.HandleResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
.WaitAndRetryAsync(3, retry => TimeSpan.FromSeconds(Math.Pow(2, retry)))); Yes! Create IAiFeedbackAnalyzer implementations for both and use DI feature flags to switch.
Add [Authorize] with JWT, rate limiting via AspNetCoreRateLimit, and validate inputs with FluentValidation.
~400 input tokens per analysis × $0.15/1M tokens = ~$0.10 per 1K analyses. Cache aggressively.
Use CompleteChatStreamingAsync() and Response.StartAsync() for real-time UI updates.
Perfect for Azure Container Apps + Azure OpenAI (same SDK). Use Managed Identity for keys.
Mock OpenAIClient or use fixed prompt responses in integration tests.
200-800ms per call. Use async/await everywhere and consider client-side caching.
You might also like these
AI-Native .NET: Building Intelligent Applications with Azure OpenAI, Semantic Kernel, and ML.NET
AI-Augmented .NET Backends: Building Intelligent, Agentic APIs with ASP.NET Core and Azure OpenAI
Master Effortless Cloud-Native .NET Microservices Using DAPR, gRPC & Azure Kubernetes Service
Build an AI chat app with .NET (Microsoft Learn) — Quickstart showing how to use OpenAI or Azure OpenAI models with .NET.
🔗 https://learn.microsoft.com/en-us/dotnet/ai/quickstarts/build-chat-app
Develop .NET apps with AI features (Microsoft Learn) — Overview of AI integration in .NET apps (APIs, services, tooling).
🔗 https://learn.microsoft.com/en-us/dotnet/ai/overview
AI-Powered Group Chat sample with SignalR + OpenAI (Microsoft Learn) — Demonstrates real-time chat with AI in an ASP.NET Core app.
🔗 https://learn.microsoft.com/en-us/aspnet/core/tutorials/ai-powered-group-chat/ai-powered-group-chat?view=aspnetcore-9.0
.NET 8 and Angular Apps with Keycloak In the rapidly evolving landscape of 2026, identity…
Mastering .NET 10 and C# 13: Building High-Performance APIs Together Executive Summary In modern…
NET 10 is the Ultimate Tool for AI-Native Founders The 2026 Lean .NET SaaS Stack…
Modern .NET development keeps pushing toward simplicity, clarity, and performance. With C# 12+, developers can…
Implementing .NET 10 LTS Performance Optimizations: Build Faster Enterprise Apps Together Executive Summary…
Building Production-Ready Headless Architectures with API-First .NET Executive Summary Modern applications demand flexibility across…
This website uses cookies.