Generative AI with .NET

Generative AI with .NET

Generative AI with .NET means building applications with C# and .NET that can use AI models to generate new content such as text, code, summaries, answers, conversations, structured data, images, and other outputs.

Traditional software usually follows explicitly programmed rules:

User
  ↓
.NET Application
  ↓
Business Logic
  ↓
Database
  ↓
Response

Generative AI adds an AI model to that workflow:

User
  ↓
.NET Application
  ↓
Prompt / Context
  ↓
Generative AI Model
  ↓
Generated Output
  ↓
.NET Application
  ↓
User

Modern .NET provides AI abstractions through Microsoft.Extensions.AI, including IChatClient, while provider-specific .NET SDKs can connect applications to services such as OpenAI and Azure OpenAI. Microsoft's current .NET AI quickstarts use .NET 8 or later and demonstrate these abstractions for prompting and chat applications.


1. What is Generative AI?

Generative AI is a type of artificial intelligence that produces new output based on an input request and the context supplied to the model.

For example:

Input:
Explain dependency injection in .NET.

AI Model:
Generates an explanation.

The output is not simply a predefined string stored in your application.

The model generates a response based on its learned model parameters and the instructions and context provided with the request.

Generative AI can generate:

  • Text

  • Code

  • Summaries

  • Questions

  • Explanations

  • Emails

  • Documentation

  • Structured data

  • Images

  • Audio

  • Other supported content

For a .NET developer, this means C# applications can incorporate generative capabilities into existing business applications.


2. What is Generative AI with .NET?

Generative AI with .NET is the combination of:

.NET / C#
       +
AI Model
       +
AI SDK / API
       +
Application Logic

A simple architecture looks like:

               USER
                 │
                 ▼
        ┌─────────────────┐
        │ .NET Application│
        └────────┬────────┘
                 │
                 ▼
             Prompt
                 │
                 ▼
        ┌─────────────────┐
        │   AI Model      │
        │      LLM        │
        └────────┬────────┘
                 │
                 ▼
         Generated Output
                 │
                 ▼
        ┌─────────────────┐
        │ .NET Application│
        └────────┬────────┘
                 │
                 ▼
                USER

The .NET application is responsible for the application architecture, authentication, business logic, data access, validation, and integration.

The AI model provides the generative capability.


3. Traditional AI vs Generative AI

Traditional machine learning applications often focus on tasks such as prediction or classification.

For example:

Customer Data
      ↓
Machine Learning Model
      ↓
Prediction

Generative AI works differently:

User Prompt
      ↓
Generative AI Model
      ↓
New Content

Example

A traditional classification system might determine:

Customer message:
"I cannot log in."

Category:
Account

A generative AI system could generate:

"Here are the steps you can follow to reset your password..."

The two approaches can also be combined.

For example:

.NET Application
       ↓
Classification
       ↓
Select Workflow
       ↓
Generative AI
       ↓
Response

4. What is an LLM?

An LLM, or Large Language Model, is a model designed to process and generate natural language.

Examples of tasks supported by language models include:

Question Answering
Text Generation
Summarization
Translation
Classification
Code Generation
Information Extraction
Conversation

A simplified architecture is:

Your .NET Application
        ↓
       LLM
        ↓
Generated Text

The .NET application does not need to implement the language model itself.

Instead, it communicates with an AI service or a locally hosted model.


5. What is the Role of .NET?

.NET provides the application infrastructure around the AI model.

For example:

                .NET AI Application
                        │
       ┌────────────────┼────────────────┐
       ▼                ▼                ▼
    ASP.NET Core     Database          AI Model
       │                │                │
       ▼                ▼                ▼
 Authentication      EF Core         AI SDK/API
       │                │                │
       └────────────────┼────────────────┘
                        ▼
                    AI Result

This means you can combine generative AI with:

  • ASP.NET Core

  • Entity Framework Core

  • SQL Server

  • PostgreSQL

  • Redis

  • Blazor

  • .NET MAUI

  • SignalR

  • Background Services

  • REST APIs

  • Cloud services

  • Authentication and authorization

Generative AI therefore becomes one component of the larger .NET application.


6. How Generative AI Works

A typical request follows this process:

1. User enters a request
          ↓
2. .NET receives the request
          ↓
3. Application creates prompt/messages
          ↓
4. AI client sends request
          ↓
5. AI model processes the request
          ↓
6. Model generates output
          ↓
7. .NET receives the response
          ↓
8. Application validates/processes output
          ↓
9. User receives the result

For example:

User:
Explain C# async/await

        ↓

ASP.NET Core

        ↓

IChatClient

        ↓

LLM

        ↓

Generated explanation

        ↓

ASP.NET Core

        ↓

Browser

7. Generative AI Technologies in .NET

There are several approaches to integrating generative AI into .NET applications.

Direct REST API

C#
 ↓
HTTP
 ↓
AI API
 ↓
JSON
 ↓
C#

This provides maximum control but requires more low-level code.

Provider SDK

C#
 ↓
AI SDK
 ↓
AI Provider
 ↓
Model

Provider SDKs handle much of the HTTP and serialization work.

Microsoft.Extensions.AI

.NET Application
       ↓
IChatClient
       ↓
AI Provider
       ↓
AI Model

Microsoft.Extensions.AI provides common abstractions such as IChatClient and IEmbeddingGenerator, allowing AI-consuming application code to work against common interfaces rather than being tightly coupled to a specific provider.

Local AI

.NET
 ↓
Ollama / Local Runtime
 ↓
Local Model

Microsoft's .NET AI documentation also provides a local-model quickstart using Ollama.


8. Setting Up a .NET Generative AI Project

For this tutorial, we will use a console application and an OpenAI-compatible integration through the .NET AI abstractions.

You need:

  • .NET 8 or later

  • Visual Studio or Visual Studio Code

  • An AI provider account if using a hosted model

  • An API credential

  • Basic C# knowledge

Microsoft's current AI quickstart uses .NET 8.0 or later for its prompt and chat examples.


9. Create the Project

Open a terminal:

dotnet new console -n GenerativeAIWithDotNet

Move into the project:

cd GenerativeAIWithDotNet

10. Install the Required Packages

For an OpenAI integration using the .NET AI abstraction:

dotnet add package OpenAI
dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease

Microsoft's current .NET AI quickstart uses the OpenAI package together with Microsoft.Extensions.AI.OpenAI; package release status can change over time, so check the current package versions when publishing or updating the tutorial.


11. Configure the API Key

Do not place your real API key directly in source code.

For a temporary Windows PowerShell session:

$env:OPENAI_API_KEY="your-api-key"

Your application can read it using:

string apiKey =
    Environment.GetEnvironmentVariable("OPENAI_API_KEY")
    ?? throw new InvalidOperationException(
        "OPENAI_API_KEY is not configured.");

For local development, .NET User Secrets can also be used.

For production, use an appropriate secret-management system or secure environment configuration.


12. Your First Generative AI Program

Replace Program.cs with:

using Microsoft.Extensions.AI;
using OpenAI;

string apiKey =
    Environment.GetEnvironmentVariable("OPENAI_API_KEY")
    ?? throw new InvalidOperationException(
        "OPENAI_API_KEY is not configured.");

IChatClient chatClient =
    new OpenAIClient(apiKey)
        .GetChatClient("gpt-5.2")
        .AsIChatClient();

var response = await chatClient.GetResponseAsync(
    "Explain dependency injection in .NET in simple words."
);

Console.WriteLine(response.Text);

The model name in the example is only an example. Replace it with a model that is available to your provider account and suitable for your application.

The current OpenAI .NET library exposes a ChatClient, and Microsoft provides an AsIChatClient adapter so that it can be consumed through the IChatClient abstraction.

Run the application:

dotnet run

The AI will generate a response to the prompt.


13. Understanding the Code

Import Microsoft.Extensions.AI

using Microsoft.Extensions.AI;

This gives access to the AI abstraction layer.

The important abstraction for generative chat is:

IChatClient

IChatClient provides methods for sending chat messages and also supports streaming responses.


Import OpenAI

using OpenAI;

This provides the provider-specific .NET client.


Read the credential

string apiKey =
    Environment.GetEnvironmentVariable("OPENAI_API_KEY")
    ?? throw new InvalidOperationException(
        "OPENAI_API_KEY is not configured.");

The key comes from configuration rather than being stored inside source code.


Create the client

IChatClient chatClient =
    new OpenAIClient(apiKey)
        .GetChatClient("gpt-5.2")
        .AsIChatClient();

This performs three conceptual operations:

OpenAIClient
     ↓
ChatClient
     ↓
IChatClient

The advantage is that the rest of the application can depend on IChatClient.


Generate the response

var response = await chatClient.GetResponseAsync(
    "Explain dependency injection in .NET in simple words."
);

The prompt is sent to the AI model.

The result is represented by a ChatResponse, whose properties include generated text and information such as the model ID and usage details.


14. What is a Prompt?

A prompt is the input instruction given to the model.

Simple prompt:

string prompt =
    "What is ASP.NET Core?";

More specific prompt:

string prompt = """
Explain ASP.NET Core to a beginner.

Include:
1. Definition
2. Main features
3. A simple C# example
4. Common use cases
5. Advantages
""";

Prompt quality can strongly affect the usefulness of generated output.


15. Prompt vs Application Logic

It is important to understand that prompts are not a replacement for application logic.

For example, do not rely only on the prompt:

"Always approve a refund if the customer asks."

Instead:

User
 ↓
.NET Application
 ↓
Authentication
 ↓
Business Rules
 ↓
AI
 ↓
Validated Output

Your application should remain responsible for important business rules.

AI can assist with interpretation and generation, while deterministic code should control critical operations.


16. Types of Prompts

Common prompt patterns include:

Instruction Prompt

Explain dependency injection in .NET.

Role-Oriented Prompt

You are a .NET programming tutor.
Explain dependency injection to a beginner.

Structured Prompt

Analyze this customer message.

Return:
Category
Priority
Summary
RecommendedAction

Context-Based Prompt

Answer the question using the following document:

DOCUMENT:
...

QUESTION:
...

Few-Shot Prompt

Provide examples first:

Example 1:
Input: ...
Output: ...

Example 2:
Input: ...
Output: ...

Now process:
Input: ...

Few-shot prompting can help guide output style or task behavior, but it does not guarantee correct results.


17. System Messages, User Messages and Assistant Messages

AI applications often work with conversations made up of messages with different roles.

A conceptual conversation is:

System
   ↓
"You are a .NET expert."

User
   ↓
"Explain dependency injection."

Assistant
   ↓
"Dependency injection is..."

With Microsoft.Extensions.AI, ChatMessage can represent messages with a ChatRole and text content.

Example:

using Microsoft.Extensions.AI;

var messages = new List<ChatMessage>
{
    new(
        ChatRole.System,
        "You are an experienced .NET programming tutor."
    ),

    new(
        ChatRole.User,
        "Explain dependency injection in simple words."
    )
};

var response =
    await chatClient.GetResponseAsync(messages);

Console.WriteLine(response.Text);

This is more flexible than sending one plain string because the application can construct a conversation explicitly.


18. Chat History

Generative AI applications often maintain conversation history.

For example:

User:
My name is Rahul.

AI:
Nice to meet you, Rahul.

User:
What is dependency injection?

AI:
Dependency injection is...

User:
Explain it using my name.

AI:
Sure, Rahul...

The application can maintain messages:

var messages = new List<ChatMessage>
{
    new(
        ChatRole.System,
        "You are a helpful .NET programming tutor."
    ),

    new(
        ChatRole.User,
        "My name is Rahul."
    ),

    new(
        ChatRole.Assistant,
        "Nice to meet you, Rahul."
    ),

    new(
        ChatRole.User,
        "Explain dependency injection."
    )
};

var response =
    await chatClient.GetResponseAsync(messages);

Console.WriteLine(response.Text);

The important concept is that the AI model generally sees the messages supplied in the request. Your application therefore has to decide how much conversation history to retain and send.

This becomes important when conversations become large.


19. What Are Tokens?

Language models process text in units called tokens.

A token is not necessarily one complete word.

For example:

Hello

may be represented as one or more tokens depending on the tokenizer.

Conceptually:

Text
 ↓
Tokenizer
 ↓
Tokens
 ↓
Model
 ↓
Generated Tokens
 ↓
Text

Tokens matter because AI models commonly have limits and usage measurements based on tokens.

For a long prompt:

Large Document
      ↓
Many Tokens
      ↓
Higher Context Requirement

Therefore, sending unnecessary information can affect cost, latency, and model context usage.


20. What is Context?

Context is the information supplied to the AI model for a particular request.

For example:

System Instructions
+
Conversation History
+
User Question
+
Retrieved Documents
+
Tool Results

may all become part of the model's context.

A RAG application can produce:

Question
+
Retrieved Documents
+
Instructions

and provide those to the model.

This is one reason context management becomes critical in large AI applications.


21. Controlling Output Length

You can provide options for a request.

For example:

var options = new ChatOptions
{
    MaxOutputTokens = 400
};

var response =
    await chatClient.GetResponseAsync(
        "Explain ASP.NET Core middleware.",
        options
    );

Console.WriteLine(response.Text);

Microsoft's current .NET AI quickstart demonstrates ChatOptions with MaxOutputTokens for controlling generated output length.

The exact behavior of individual options can depend on the model and provider.


22. Temperature and Randomness

Many generative AI systems expose a temperature-like setting controlling how much variability is allowed in generation.

Conceptually:

Lower randomness
      ↓
More predictable output

and:

Higher randomness
      ↓
More varied output

For deterministic business applications, you may prefer conservative generation settings.

For creative writing, brainstorming, or idea generation, more variation may be useful.

However, model providers and newer model families do not all expose or interpret temperature in exactly the same way, so do not assume that every model supports identical generation controls.


23. Text Generation Example

Let's build a reusable method:

public async Task<string> GenerateTextAsync(
    IChatClient chatClient,
    string prompt,
    CancellationToken cancellationToken = default)
{
    var response =
        await chatClient.GetResponseAsync(
            prompt,
            cancellationToken: cancellationToken);

    return response.Text;
}

Now the method can generate many types of content:

string result = await GenerateTextAsync(
    chatClient,
    "Write a short introduction to ASP.NET Core."
);

Console.WriteLine(result);

24. AI Summarization with .NET

Suppose you have an article:

string article = """
ASP.NET Core is a cross-platform framework for
building modern web applications and APIs.
It supports dependency injection, middleware,
routing, authentication, logging and configuration.
""";

Create a summarization prompt:

string prompt = $"""
Summarize this text in three bullet points.

TEXT:
{article}
""";

var response =
    await chatClient.GetResponseAsync(prompt);

Console.WriteLine(response.Text);

The same approach can be used for:

  • Articles

  • Emails

  • Support tickets

  • Meeting notes

  • Documentation

  • Reports

Generative AI with .NET

25. AI Translation with .NET

Example:

string text =
    "ASP.NET Core is a cross-platform web framework.";

string prompt = $"""
Translate the following English sentence into Hindi.

Text:
{text}
""";

var response =
    await chatClient.GetResponseAsync(prompt);

Console.WriteLine(response.Text);

AI translation can be useful in multilingual applications.

For high-stakes content, human review or specialized translation workflows may still be necessary.


26. AI Classification with .NET

Suppose you receive:

string message =
    "My credit card was charged twice.";

Ask the model to classify it:

string prompt = $"""
Classify this support request.

Allowed categories:
Billing
Account
Technical
General

Return only the category.

Request:
{message}
""";

var response =
    await chatClient.GetResponseAsync(prompt);

Console.WriteLine(response.Text);

The model might return:

Billing

Your application should then validate the result:

string category = response.Text.Trim();

string[] allowed =
[
    "Billing",
    "Account",
    "Technical",
    "General"
];

if (!allowed.Contains(
        category,
        StringComparer.OrdinalIgnoreCase))
{
    category = "General";
}

This illustrates an important principle:

Never assume that generated text automatically follows your requested format.

Validate model output before using it as application data.


27. AI Code Generation

Generative AI can also generate C# code.

Example:

string prompt = """
Create a C# class called ProductService.

Requirements:
- Use dependency injection.
- Use asynchronous programming.
- Add GetProductsAsync().
- Return a list of Product objects.
- Include suitable error handling.
""";

var response =
    await chatClient.GetResponseAsync(prompt);

Console.WriteLine(response.Text);

The generated code should be:

Generated
   ↓
Reviewed
   ↓
Compiled
   ↓
Tested
   ↓
Security Checked
   ↓
Used

AI-generated code should not be treated as automatically correct.


28. Structured Output

Many business applications need data rather than free-form text.

For example, instead of:

"The customer seems unhappy and the issue
is probably related to billing."

you may want:

{
  "category": "Billing",
  "sentiment": "Negative",
  "priority": "High"
}

Structured output allows applications to work with model-generated data more reliably.

The current .NET AI abstractions include generic GetResponseAsync<T> overloads for requesting a response matching a .NET type, subject to support from the underlying provider.

A conceptual model is:

public class SupportAnalysis
{
    public string Category { get; set; } = "";
    public string Sentiment { get; set; } = "";
    public string Priority { get; set; } = "";
}

Then the application can request structured data using the supported structured-output API.

Microsoft also provides a dedicated .NET quickstart for structured model output.


29. Streaming Generative AI Responses

Normally, the application waits until the complete response is generated:

Request
 ↓
Wait
 ↓
Complete Response

Streaming changes the experience:

Request
 ↓
Token / chunk
 ↓
Token / chunk
 ↓
Token / chunk
 ↓
More output
 ↓
Complete

This is especially useful for chat interfaces.

IChatClient provides GetStreamingResponseAsync for streaming chat responses.

Example:

await foreach (
    var update in chatClient.GetStreamingResponseAsync(
        "Explain ASP.NET Core middleware."
    ))
{
    Console.Write(update.Text);
}

The user can see the answer arriving progressively instead of waiting for the entire answer.


30. Generative AI with ASP.NET Core

A real application commonly places generative AI behind an ASP.NET Core API.

Architecture:

Browser
   ↓
ASP.NET Core API
   ↓
AI Service
   ↓
IChatClient
   ↓
AI Model

Example request:

POST /api/ai/generate
Content-Type: application/json

Request:

{
  "prompt": "Explain dependency injection in .NET"
}

Response:

{
  "response": "Dependency injection is..."
}

31. ASP.NET Core Generative AI Example

Create the project:

dotnet new web -n GenerativeAIWebApi
cd GenerativeAIWebApi

Install:

dotnet add package OpenAI
dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease

Configure:

$env:OpenAI__ApiKey="your-api-key"

Then:

using Microsoft.Extensions.AI;
using OpenAI;

var builder = WebApplication.CreateBuilder(args);

string apiKey =
    builder.Configuration["OpenAI:ApiKey"]
    ?? throw new InvalidOperationException(
        "OpenAI API key is not configured.");

builder.Services.AddSingleton<IChatClient>(_ =>
    new OpenAIClient(apiKey)
        .GetChatClient("gpt-5.2")
        .AsIChatClient());

var app = builder.Build();

app.MapPost(
    "/api/ai/generate",
    async (
        GenerateRequest request,
        IChatClient chatClient,
        CancellationToken cancellationToken) =>
    {
        if (string.IsNullOrWhiteSpace(request.Prompt))
        {
            return Results.BadRequest(
                new
                {
                    error = "Prompt is required."
                });
        }

        var response =
            await chatClient.GetResponseAsync(
                request.Prompt,
                cancellationToken: cancellationToken);

        return Results.Ok(
            new GenerateResponse(response.Text));
    });

app.Run();

public record GenerateRequest(string Prompt);

public record GenerateResponse(string Response);

The API can now receive:

{
  "prompt": "Explain C# interfaces."
}

and return generated content.

In a production application, add authentication, authorization, rate limiting, request-size limits, logging, error handling, usage controls, and appropriate monitoring.


32. Building a Reusable AI Service

Instead of putting AI logic directly into the endpoint:

Controller
 ↓
IChatClient

use a service layer:

Controller
     ↓
IGenerativeAIService
     ↓
IChatClient
     ↓
AI Model

Interface:

public interface IGenerativeAIService
{
    Task<string> GenerateAsync(
        string prompt,
        CancellationToken cancellationToken = default);
}

Implementation:

using Microsoft.Extensions.AI;

public class GenerativeAIService
    : IGenerativeAIService
{
    private readonly IChatClient _chatClient;

    public GenerativeAIService(
        IChatClient chatClient)
    {
        _chatClient = chatClient;
    }

    public async Task<string> GenerateAsync(
        string prompt,
        CancellationToken cancellationToken = default)
    {
        var response =
            await _chatClient.GetResponseAsync(
                prompt,
                cancellationToken: cancellationToken);

        return response.Text;
    }
}

Register it:

builder.Services.AddScoped<
    IGenerativeAIService,
    GenerativeAIService>();

Now the API can use:

IGenerativeAIService aiService

instead of directly depending on the AI provider.

This is a much better architecture for larger applications.


33. .NET Configuration for Generative AI

Instead of putting configuration directly in source code:

new OpenAIClient(apiKey)

you can centralize configuration:

{
  "AI": {
    "Model": "gpt-5.2"
  }
}

and access:

string model =
    builder.Configuration["AI:Model"]
    ?? throw new InvalidOperationException(
        "AI model is not configured.");

Do not store production secrets in appsettings.json.

Use:

Development
    ↓
User Secrets

Production
    ↓
Environment Variables
or
Secret Management

34. Generative AI and Dependency Injection

Dependency injection makes provider changes easier.

For example:

Application
     ↓
IChatClient
     ↓
OpenAI

Later you could change the implementation:

Application
     ↓
IChatClient
     ↓
Azure OpenAI

or:

Application
     ↓
IChatClient
     ↓
Ollama

The core application services can continue depending on IChatClient.

This is one of the main benefits of using common AI abstractions.

Microsoft specifically describes Microsoft.Extensions.AI as a unified approach for integrating different AI services while allowing consuming code to work with common interfaces.


35. Cloud Generative AI vs Local Generative AI

There are two broad deployment patterns.

Cloud AI

.NET Application
      ↓
Internet
      ↓
Cloud AI Service
      ↓
Model

Advantages can include:

  • Managed infrastructure

  • Access to hosted models

  • Easy scaling

  • No local model hosting

Considerations include:

  • Network dependency

  • Provider pricing

  • Data governance

  • Service limits

Local AI

.NET Application
      ↓
Local Runtime
      ↓
Local Model

Advantages can include:

  • Local model execution

  • More control over deployment

  • Reduced dependency on a remote AI service

Considerations include:

  • Hardware requirements

  • Model performance

  • Local operations

  • Model management

Microsoft provides an official .NET quickstart for connecting to a local model through Ollama.


36. Generative AI + Databases

Generative AI becomes more useful when integrated with application data.

For example:

User
 ↓
.NET
 ↓
Business Logic
 ↓
SQL Server
 ↓
Relevant Data
 ↓
AI Model
 ↓
Generated Answer

A customer might ask:

How many orders did customer 1001 place this month?

The application could retrieve the required data and then generate a natural-language explanation.

Possible technologies include:

Entity Framework Core
Dapper
SQL Server
PostgreSQL
MongoDB
Redis

However, do not give a language model unrestricted access to your production database.

Use:

  • restricted database accounts

  • approved functions

  • parameterized queries

  • authorization

  • validation

  • auditing

  • read-only access where appropriate


37. Generative AI + RAG

A plain generative AI application works like:

Question
   ↓
LLM
   ↓
Answer

RAG adds retrieval:

Question
   ↓
Search
   ↓
Relevant Documents
   ↓
LLM
   ↓
Answer

For example:

Company Employee
        ↓
"What is the leave policy?"
        ↓
.NET RAG Application
        ↓
Search Company Documents
        ↓
Relevant Policy
        ↓
Generative AI
        ↓
Answer

Generative AI is therefore the generation component, while RAG supplies external context.

RAG will be covered as a separate major topic later in the course.


38. Generative AI + Tools

Generative AI can also work together with application tools.

For example:

User:
What is my order status?

The workflow can become:

User
 ↓
AI Model
 ↓
Request approved order lookup tool
 ↓
.NET
 ↓
OrderService
 ↓
SQL Server
 ↓
Order result
 ↓
AI Model
 ↓
Natural-language response

The AI generates the final response, while .NET remains responsible for the actual business operation.

This pattern leads into:

Function Calling
        ↓
Tool Calling
        ↓
AI Agents

39. Generative AI + Images

Generative AI isn't limited to text.

Depending on the provider and model, .NET applications can also interact with image-capable AI services.

A simplified architecture is:

Text Prompt
    ↓
.NET
    ↓
Image Model
    ↓
Generated Image
    ↓
.NET
    ↓
User

Microsoft's .NET AI documentation includes quickstarts covering image generation and additional generative AI capabilities.


40. Generative AI + Voice

Another extension is voice:

User Speech
      ↓
Speech-to-Text
      ↓
.NET
      ↓
Generative AI
      ↓
Text Response
      ↓
Text-to-Speech
      ↓
User

This can be used for:

  • Voice assistants

  • Interview applications

  • Customer support

  • Accessibility applications

  • Voice-controlled applications

Speech capabilities depend on the selected provider and model.


41. Generative AI + Blazor

Blazor can provide a web interface for an AI application.

Architecture:

Blazor
  ↓
ASP.NET Core
  ↓
Generative AI Service
  ↓
LLM

A simple UI could contain:

┌────────────────────────────┐
│       AI Assistant         │
│                            │
│ Ask your question:         │
│                            │
│ [ Explain LINQ in C# ]     │
│                            │
│ [Ask AI]                   │
│                            │
│ AI Response:               │
│ ...                        │
└────────────────────────────┘

The API should generally keep the provider credential on the server rather than exposing it to browser code.


42. Generative AI + .NET MAUI

.NET MAUI can provide the frontend for a mobile or desktop AI application.

A common architecture is:

.NET MAUI
    ↓
ASP.NET Core API
    ↓
AI Service
    ↓
LLM

For example:

Mobile Application
       ↓
Secure API
       ↓
Generative AI
       ↓
Response

The backend can manage credentials, authentication, business logic, and AI access.


43. Error Handling

AI applications must expect failures.

Potential problems include:

Invalid API credentials
Model unavailable
Rate limit
Timeout
Network failure
Invalid request
Context limit
Provider error

A basic pattern is:

try
{
    var response =
        await chatClient.GetResponseAsync(
            prompt,
            cancellationToken: cancellationToken);

    return response.Text;
}
catch (OperationCanceledException)
{
    throw;
}
catch (Exception)
{
    throw new InvalidOperationException(
        "The AI request could not be completed.");
}

In production, catch provider-specific exceptions where possible, log useful diagnostic information securely, and avoid exposing internal details to users.


44. Security Considerations

Generative AI introduces additional security concerns.

API Key Protection

Never expose:

API Key

to browser JavaScript or client applications when the key is intended to remain server-side.

Prompt Injection

Users may attempt to manipulate an AI system through malicious or conflicting instructions.

For example:

Ignore your previous instructions...

Your application must not assume that prompts themselves are a secure authorization mechanism.

Tool Security

If AI can invoke tools:

AI
 ↓
C# Function
 ↓
Database

the function must enforce its own authorization.

Data Privacy

Avoid sending sensitive information to an external AI service unless your architecture, provider agreement, and applicable data-handling requirements permit it.


45. Performance Considerations

AI applications can be slower than ordinary in-process methods because requests may involve network communication and model inference.

Consider:

Async / Await
Streaming
Connection Reuse
Context Reduction
Caching
Rate Limiting
Timeouts

For example:

Large Prompt
      ↓
Many Tokens
      ↓
More Processing
      ↓
Higher Latency

Only send the context the model actually needs.


46. Cost Considerations

Hosted AI services often charge based on usage or service configuration.

Possible cost drivers include:

Input Tokens
Output Tokens
Model Selection
Request Volume
Image Generation
Audio Processing
Tool Usage

Therefore:

Measure
   ↓
Optimize
   ↓
Monitor
   ↓
Control

Applications should track usage and establish appropriate limits.


47. Testing Generative AI Applications

Testing AI applications differs from testing ordinary deterministic code.

For normal code:

Input
 ↓
Expected Output

For generative AI:

Input
 ↓
AI Model
 ↓
Generated Output

The wording can vary.

Therefore, test both:

Application behavior

Validation
Authentication
Authorization
Database operations
API behavior
Error handling

AI behavior

Correctness
Relevance
Format
Safety
Consistency
Grounding

For important applications, create representative evaluation datasets rather than relying on one manual prompt.


48. Common Mistakes

Mistake 1: Hard-coding API keys

Bad:

string apiKey = "secret";

Use secure configuration instead.


Mistake 2: Trusting AI output blindly

AI output can be incorrect.

Always validate data before using it for important operations.


Mistake 3: Giving AI unrestricted database access

Never assume a generated SQL statement is safe simply because it came from an AI model.


Mistake 4: Sending huge amounts of unnecessary context

Large prompts increase token usage and can reduce application efficiency.


Mistake 5: Putting all AI logic in controllers

Prefer:

Controller
 ↓
Service
 ↓
IChatClient
 ↓
AI

instead of putting the entire implementation into one controller.


Mistake 6: Treating generative AI as deterministic

Two otherwise identical AI requests may not always produce identical wording.

Design your application with that behavior in mind.


49. Real-World Generative AI Applications in .NET

You can build:

AI Customer Support

Customer
 ↓
ASP.NET Core
 ↓
AI
 ↓
Support Response

AI Resume Analyzer

Resume
 ↓
.NET
 ↓
AI
 ↓
Skills / Summary / Analysis

AI Interview Assistant

Candidate
 ↓
.NET
 ↓
AI
 ↓
Question
 ↓
Answer
 ↓
AI Evaluation

AI Documentation Assistant

Documentation
 ↓
RAG
 ↓
Generative AI
 ↓
Answer

AI SQL Assistant

Natural Language
 ↓
.NET
 ↓
Controlled SQL Tool
 ↓
Database
 ↓
AI Explanation

AI Coding Assistant

Developer
 ↓
.NET
 ↓
AI
 ↓
Code / Explanation / Suggestions

50. Generative AI Architecture

A simple production-oriented architecture can look like:

                         USER
                           │
                           ▼
                  ┌─────────────────┐
                  │   Web / Mobile  │
                  └────────┬────────┘
                           │
                           ▼
                  ┌─────────────────┐
                  │  ASP.NET Core   │
                  │      API        │
                  └────────┬────────┘
                           │
              ┌────────────┼────────────┐
              ▼            ▼            ▼
          Auth/Rules      AI         Database
                           │
                           ▼
                     IChatClient
                           │
                           ▼
                     AI Provider
                           │
                           ▼
                       LLM Model

An advanced architecture may become:

                            USER
                             │
                             ▼
                       ASP.NET Core
                             │
                 ┌───────────┼───────────┐
                 ▼           ▼           ▼
               RAG         Tools       Memory
                 │           │           │
                 ▼           ▼           ▼
             Vector DB     APIs        Storage
                 │           │           │
                 └───────────┼───────────┘
                             ▼
                         AI Agent
                             │
                             ▼
                            LLM
                             │
                             ▼
                         Response

51. Generative AI Learning Path

For your .NET + AI course, the recommended progression is:

Generative AI with .NET
          ↓
LLMs with .NET
          ↓
Prompt Engineering
          ↓
OpenAI with .NET
          ↓
Azure OpenAI with .NET
          ↓
Microsoft.Extensions.AI
          ↓
Chat Applications
          ↓
Streaming
          ↓
Structured Output
          ↓
Embeddings
          ↓
Vector Databases
          ↓
RAG
          ↓
Function Calling
          ↓
Tool Calling
          ↓
MCP
          ↓
AI Agents
          ↓
Multi-Agent Systems
          ↓
AI Automation
          ↓
Production AI

This follows the overall progression in Microsoft's current .NET AI learning material from prompting and chat toward structured output, data interaction, tools, RAG, and agent scenarios.


52. Frequently Asked Questions

Q1. What is Generative AI?

Generative AI is AI that can generate new content from instructions and context, such as text, code, images, or other supported outputs.

Q2. What is Generative AI with .NET?

It means integrating generative AI models into applications built with C# and .NET.

Q3. Can C# be used for Generative AI?

Yes. C# applications can communicate with AI services through .NET SDKs, REST APIs, common AI abstractions, and local AI runtimes.

Q4. What is an LLM?

An LLM is a large language model designed to process and generate natural language.

Q5. What is IChatClient?

IChatClient is a common .NET abstraction for chat-based AI interactions. It supports normal responses as well as streaming responses.

Q6. What is Microsoft.Extensions.AI?

It is a set of .NET libraries providing common abstractions for integrating generative AI services, including chat and embedding functionality.

Q7. Can we use OpenAI with .NET?

Yes. OpenAI provides an official .NET library, and the OpenAI client types can be adapted to IChatClient.

Q8. Can we use Azure OpenAI with .NET?

Yes. Microsoft provides .NET integrations for Azure OpenAI, including Microsoft.Extensions.AI integration.

Q9. Can we use local AI with .NET?

Yes. .NET can connect to local AI runtimes such as Ollama.

Q10. What is a prompt?

A prompt is the instruction or input provided to an AI model.

Q11. What are tokens?

Tokens are units used by language models to represent text during processing and generation.

Q12. What is context?

Context is the information supplied to the model for a particular request, such as instructions, conversation history, retrieved documents, and other relevant information.

Q13. What is streaming?

Streaming allows generated content to be delivered progressively instead of waiting for the complete response. IChatClient provides streaming APIs for this scenario.

Q14. What is structured output?

Structured output means asking the model to produce data that conforms to a defined structure, such as a JSON object or .NET type, instead of unrestricted prose. .NET provides APIs for requesting typed responses where the underlying provider supports them.

Q15. What is RAG?

RAG stands for Retrieval-Augmented Generation. It retrieves relevant external information and supplies it to a generative model before generating an answer.

Q16. What is the difference between RAG and Generative AI?

Generative AI generates the response.

RAG adds a retrieval step that supplies external information.

Generative AI:

Question
 ↓
LLM
 ↓
Answer
RAG:

Question
 ↓
Retrieve
 ↓
Context
 ↓
LLM
 ↓
Answer

Q17. Can Generative AI generate C# code?

Yes. It can generate code, explanations, tests, documentation, and other programming-related content. Generated code should still be reviewed and tested.

Q18. Can Generative AI work with SQL Server?

Yes. A .NET application can combine AI with SQL Server, but database access should be controlled by application code and proper authorization.

Q19. Can Generative AI work with Blazor?

Yes. Blazor can provide the frontend for an AI application, with ASP.NET Core handling secure backend communication.

Q20. Can Generative AI work with .NET MAUI?

Yes. A .NET MAUI application can communicate with a secure ASP.NET Core backend that provides AI functionality.


53. Generative AI Interview Questions

1. What is Generative AI?

Generative AI produces new content based on an input prompt and context.

2. What is an LLM?

An LLM is a large language model capable of understanding and generating natural language.

3. How do you integrate Generative AI into a .NET application?

A .NET application can use a provider SDK, REST API, or an abstraction such as IChatClient to send prompts and receive generated responses.

4. What is Microsoft.Extensions.AI?

It is a common AI abstraction layer for .NET applications that provides interfaces and integrations for AI services.

5. Why use IChatClient?

It allows application services to depend on a common chat abstraction rather than directly depending on a specific provider implementation.

6. What is a prompt?

A prompt is an instruction or input sent to an AI model.

7. What is context?

Context is the information made available to the model for a particular request.

8. What are tokens?

Tokens are units used by the model to process text.

9. What is streaming?

Streaming allows AI-generated output to be returned incrementally.

10. What is structured output?

Structured output asks the AI system to return information in a predefined structure, such as a JSON schema or .NET type.

11. How can you reduce AI costs?

You can reduce costs by controlling prompt size, selecting suitable models, limiting unnecessary context, monitoring usage, and caching appropriate results.

12. How do you secure an AI API key?

Keep it server-side and use secure configuration or secret-management mechanisms rather than putting it in source code or browser code.

13. Is Generative AI deterministic?

Not necessarily. Model responses can vary depending on the model, request, generation settings, and provider behavior.

14. How would you integrate AI into ASP.NET Core?

Register an AI client through dependency injection, create a dedicated AI service, expose appropriate API endpoints, and secure and validate the resulting workflow.

15. What should you do before executing AI-generated business operations?

Validate the model output, enforce application authorization, apply business rules, and restrict the operations available to the model.


54. Practical Exercises

Exercise 1 — Basic Text Generation

Build a console application that asks:

Explain dependency injection in ASP.NET Core.

and displays the response.

Exercise 2 — Summarization

Read a text file:

article.txt

and ask the model to summarize it.

Exercise 3 — Translation

Build a method:

TranslateAsync(
    string text,
    string language)

that generates a translated response.

Exercise 4 — Classification

Create a customer support classifier with:

Billing
Account
Technical
General

Exercise 5 — Chat History

Implement:

System message
+
User message
+
Assistant response
+
Next user message

Exercise 6 — Streaming

Use:

GetStreamingResponseAsync()

to display the response as it arrives.

Exercise 7 — ASP.NET Core

Create:

POST /api/ai/generate

and return the generated response as JSON.

Exercise 8 — Structured Output

Create a support-analysis model:

public class SupportAnalysis
{
    public string Category { get; set; } = "";
    public string Priority { get; set; } = "";
    public string Summary { get; set; } = "";
}

and build a structured AI response workflow.

Exercise 9 — Local AI

Connect the .NET application to an Ollama-hosted model.

Exercise 10 — Real-World Project

Build an:

AI Customer Support Assistant

with:

ASP.NET Core
       ↓
IChatClient
       ↓
Conversation History
       ↓
RAG
       ↓
Database
       ↓
AI Response

55. Generative AI Project Architecture

A complete project can eventually evolve into:

                         ┌───────────────┐
                         │     User      │
                         └───────┬───────┘
                                 │
                                 ▼
                      ┌────────────────────┐
                      │   ASP.NET Core     │
                      │        API         │
                      └─────────┬──────────┘
                                │
                   ┌────────────┼────────────┐
                   ▼            ▼            ▼
                AI Service     RAG         Memory
                   │            │            │
                   ▼            ▼            ▼
              IChatClient   Vector DB     Storage
                   │            │
                   └──────┬─────┘
                          ▼
                     AI Provider
                          │
                          ▼
                        LLM
                          │
                          ▼
                    Generated Result

Later, tools can be added:

                     AI Application
                           │
                    ┌──────┴──────┐
                    ▼             ▼
                  RAG           Tools
                    │             │
                    ▼             ├── SQL Server
               Vector DB          ├── REST API
                                  ├── Search
                                  └── Business Functions

And eventually:

                    AI Agent
                       │
          ┌────────────┼────────────┐
          ▼            ▼            ▼
         RAG         Tools        Memory
          │            │            │
          └────────────┼────────────┘
                       ▼
                      LLM
                       │
                       ▼
                  Final Result

56. Generative AI with .NET — Complete Learning Path

The concepts in this article provide the foundation for the next topics:

Generative AI with .NET
        ↓
LLMs with .NET
        ↓
Prompt Engineering with .NET
        ↓
OpenAI with .NET
        ↓
Azure OpenAI with .NET
        ↓
Microsoft.Extensions.AI
        ↓
AI Chat Applications
        ↓
Streaming AI Responses
        ↓
Structured Outputs
        ↓
Embeddings
        ↓
Vector Databases
        ↓
RAG
        ↓
Function Calling
        ↓
Tool Calling
        ↓
MCP
        ↓
AI Agents
        ↓
Multi-Agent Systems
        ↓
AI Automation
        ↓
Production AI

57. Conclusion

Generative AI allows .NET developers to build applications that can generate text, code, summaries, explanations, structured information, and other AI-generated content.

The fundamental architecture is:

.NET Application
       ↓
Prompt / Context
       ↓
AI Client
       ↓
Generative AI Model
       ↓
Generated Output
       ↓
.NET Application

As applications become more advanced, the architecture expands:

.NET
 ↓
LLM
 ↓
Chat
 ↓
Memory
 ↓
Embeddings
 ↓
Vector Search
 ↓
RAG
 ↓
Tools
 ↓
Function Calling
 ↓
MCP
 ↓
AI Agents
 ↓
Multi-Agent Systems

The most important concept for a .NET developer is that Generative AI is not a replacement for normal software development.

.NET remains responsible for:

Business Logic
Authentication
Authorization
Databases
APIs
Validation
Security
Logging
Testing
Deployment

Generative AI adds intelligent generation and reasoning capabilities to that application.

Once you understand this foundation, the next step is to understand the technology behind these applications in more detail:

AI Application Development with .NET



Post a Comment

Previous Post Next Post