Adding AI to Your .NET App with `IChatClient`

Adding AI to Your .NET App with `IChatClient`

Are you looking to add AI to your .NET app? Microsoft.Extensions.AI makes it easy!

This is the first post in a new series where we'll learn Microsoft.Extensions.AI together, one building block at a time. Let's start with the most important building block of them all: IChatClient!

You're welcome to skip the definitions and jump directly to the code below.

What is Microsoft.Extensions.AI?

Microsoft.Extensions.AI (aka "MEAI") is the recommended approach for all us .NET developers to integrate LLMs into our apps going forward.

Under the hood, MEAI provides an abstraction layer that allows us to easily swap AI providers (OpenAI, Azure, Amazon Bedrock, Ollama + more) while keeping the code in our business logic and unit tests exactly the same. Think of it like ILogger, but for AI: we write our code once, then plug in any provider.

What is IChatClient?

IChatClient is the heart of MEAI. It's an interface with two primary methods:

  • GetResponseAsync sends our messages to the LLM and returns its complete response
  • GetStreamingResponseAsync streams the response back token-by-token (the ChatGPT typewriter effect)

That's it! Every AI provider plugs into these same two methods.

Code

Now that we have those pesky definitions out of the way, let's write some code!

You can find the completed code sample here.

Prerequisites

If you'd like to follow along, here are the steps.

I recommend Ollama because it's completely free. It runs open-source models locally on our machine, so there's no API key and no credit card required!

  1. Download + install Ollama
  2. Download a model by entering this command in the terminal:
ollama pull qwen3.5

Now we can finally write some code!

Console App

First, let's create a File->New Console App:

  1. Create a File -> New Console App in Visual Studio
  2. Add the Microsoft.Extensions.AI NuGet Package to the app
  3. Add the OllamaSharp NuGet Package to the app

If you're unfamiliar with adding NuGet Packages, you can find more information here.

using Microsoft.Extensions.AI;
using OllamaSharp;

// OllamaApiClient implements IChatClient, the abstraction provided by Microsoft.Extensions.AI
IChatClient client = new OllamaApiClient(new Uri("http://localhost:11434"), "qwen3.5");

ChatResponse response = await client.GetResponseAsync("Explain async/await in one sentence.");

Console.WriteLine(response.Text);

Now let's click Run and see what gets printed to the console:

Async/await lets your code kick off a long-running task and keep working
while it finishes, without blocking the thread.

It works! We just built an AI Chat Bot in 5 lines of code!

Streaming Responses

Waiting for the complete response can feel slow. Let's make our app feel alive by streaming the response token-by-token using GetStreamingResponseAsync:

await foreach (ChatResponseUpdate update in client.GetStreamingResponseAsync("Tell me a short story about a developer attending Microsoft Build for the first time."))
{
    Console.Write(update.Text); // Prints each token as it arrives, just like ChatGPT
}

Swapping AI Providers

Here's the best part: Because we wrote our code against the IChatClient interface, we can swap Ollama for OpenAI by changing ONE line of code.

First, add the Microsoft.Extensions.AI.OpenAI NuGet Package:

dotnet add package Microsoft.Extensions.AI.OpenAI

Then update the one line where we create our IChatClient:

using Microsoft.Extensions.AI;
using OpenAI;

// Same IChatClient interface, different AI provider!
IChatClient client = new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
                        .GetChatClient("gpt-5.1")
                        .AsIChatClient();

Every other line of our app stays exactly the same. No rewrites when we change AI providers — our future selves will thank us! 💯

Conclusion

As .NET developers, we should all be leveraging Microsoft.Extensions.AI when adding GenAI to our apps. New models debut every month, and thanks to IChatClient, we'll never be locked in to yesterday's provider.

And we've only scratched the surface! IChatClient can also remember our Chat History, invoke Function Calling (yes — the LLM can call our C# methods!), and add middleware like caching, logging and telemetry.

You can find the completed code sample here: https://github.com/Dometrain/from-zero-to-hero-microsoft-extensions-ai

Next week, we'll teach our apps to search by meaning using IEmbeddingGenerator. Make sure to subscribe to this blog so you don't miss it! 👇

To learn even more about Microsoft Extensions AI, check out my comprehensive course on Dometrain:

Learn to use Microsoft.Extensions.AI in .NET - Dometrain
Master the main AI abstraction library for .NET AI development
GitHub - Dometrain/from-zero-to-hero-microsoft-extensions-ai: The accompanying source code for the DomeTrain Course “From Zero to Hero: Microsoft.Extensions.Ai”
The accompanying source code for the DomeTrain Course “From Zero to Hero: Microsoft.Extensions.Ai” - Dometrain/from-zero-to-hero-microsoft-extensions-ai