.NET  

๐Ÿš€ Building a Generative AI Microservice in .NET 8

Integrate Generative AI capabilities into your microservices using modern .NET architecture

๐Ÿง  Introduction

With the rapid rise of Generative AI, developers are no longer limited to static business logic. Applications can now create, summarize, explain, and even generate code or SQL queries dynamically.

By combining .NET 8 and Generative AI APIs (like OpenAI, Azure OpenAI, or Hugging Face), you can build intelligent microservices capable of natural language understanding and generation.

In this article, youโ€™ll learn to build a Generative AI-powered microservice using .NET 8 Minimal APIs and OpenAI GPT API.

โš™๏ธ Prerequisites

Before we begin, ensure you have the following setup:

โœ… Tools Required

  • .NET 8 SDK

  • Visual Studio 2022 / VS Code

  • OpenAI API key โ†’ https://platform.openai.com

  • (Optional) Docker for microservice containerization

๐Ÿงฉ Project Setup

Step 1. Create a New .NET 8 Web API Project

In your terminal or command prompt:

dotnet new webapi -n GenerativeAIMicroservice
cd GenerativeAIMicroservice

Step 2. Clean Up the Template

Remove default controllers and WeatherForecast examples.
Weโ€™ll use a Minimal API style for simplicity.

Step 3. Add Dependencies

Install the following NuGet packages:

dotnet add package OpenAI_API
dotnet add package Newtonsoft.Json

These allow communication with the OpenAI GPT API and handle JSON serialization.

๐Ÿงฑ Step 4. Create the AI Service

Create a new file: Services/AiService.cs

using OpenAI_API;
using System.Threading.Tasks;

namespace GenerativeAIMicroservice.Services
{
    public class AiService
    {
        private readonly OpenAIAPI _api;

        public AiService(string apiKey)
        {
            _api = new OpenAIAPI(apiKey);
        }

        public async Task<string> GenerateTextAsync(string prompt)
        {
            var result = await _api.Completions.GetCompletion(prompt);
            return result;
        }
    }
}

This service will handle all Generative API communication.

๐ŸŒ Step 5. Create the Minimal API Endpoint

In your Program.cs file, add:

using GenerativeAIMicroservice.Services;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(new AiService("sk-your-openai-api-key"));  // Replace with your key

var app = builder.Build();

app.MapPost("/api/generate", async (AiService aiService, PromptRequest request) =>
{
    if (string.IsNullOrEmpty(request.Prompt))
        return Results.BadRequest("Prompt is required.");

    var response = await aiService.GenerateTextAsync(request.Prompt);
    return Results.Ok(new { Output = response });
});

app.Run();

record PromptRequest(string Prompt);

๐Ÿง  Example Request

You can now test your Generative API microservice using Postman or curl.

POST Request

URL:

https://localhost:5001/api/generate

Body (JSON):

{"Prompt": "Write a C# function to reverse a string"}

Example Response

{"Output": "public string ReverseString(string s) { char[] arr = s.ToCharArray(); Array.Reverse(arr); return new string(arr); }"}

๐Ÿงช Step 6. Containerize with Docker (Optional)

To make it cloud-ready, create a Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "GenerativeAIMicroservice.dll"]

Then run:

docker build -t generative-ai-service .
docker run -p 8080:80 generative-ai-service

Your microservice is now containerized and ready for deployment to Azure App Service, AWS ECS, or Kubernetes.

โšก Step 7. Real-World Use Cases

ScenarioDescription
Code AssistantGenerate code snippets based on developer prompts
Chatbot BackendProvide intelligent responses in chat systems
SQL GeneratorConvert text prompts into database queries
Content CreationAuto-generate text, descriptions, and blogs
AI Documentation ServiceSummarize and document APIs automatically

๐Ÿง  Architecture Overview

Client App (UI)
     โ†“
Generative AI Microservice (.NET 8)
     โ†“
OpenAI / Azure OpenAI API
     โ†“
AI-Generated Response

This architecture makes the AI layer modular, secure, and reusable across multiple projects.

๐Ÿ” Security Considerations

โœ… Do not hardcode API keys โ€” use:

dotnet user-secrets set "OpenAIKey" "sk-your-key"

and retrieve it via:

builder.Configuration["OpenAIKey"]

โœ… Limit tokens and rate of calls
โœ… Sanitize user inputs before sending to AI API

๐ŸŒ External References

๐Ÿ Conclusion

By integrating Generative AI APIs into a .NET 8 microservice, you can bring AI-driven intelligence into any system โ€” whether for content creation, coding automation, or chatbot applications.

This architecture is modular, scalable, and ready for enterprise deployment, bridging traditional software engineering with next-generation AI development.