Skip to main content

Microsoft.Extensions.AI–Part II - ASP.NET Core Integration

Last week I finally started my journey with Microsoft.Extensions.AI after having used only Semantic Kernel for all my agentic AI workflows. I started with a short introduction on what Microsoft.Extensions.AI is and we created our first 'Hello AI' demo combining Microsoft.Extensions.AI and AI Foundry Local.

This post is part of a blog series. Other posts so far:

Most of the time you will not have your AI workloads running in a console application but integrated in an ASP.NET Core app so that is exactly what we are trying to achieve today.

Integrating Microsoft.Extensions.AI in ASP.NET Core

We’ll start simple, we want to show a Razor page where we can enter some text and let the LLM respond. Important is that the results are streamed to the frontend.

  • Start by creating a new ASP.NET Core application. Use the Razor pages template in Visual Studio:


  • We update our Program.cs file to include the same code to build up our IChatClient:
  • But now we add one extra line to register our IChatClient in the ASP.NET Core DI container:
  • Next step is to write a ‘chat’ API endpoint that takes some input and return the output from the LLM.
    • Remark: Notice that I’m using the GetStreamingResponseAsync to stream the results to the frontend so that the user doesn’t have to wait until the full response is generated.
  • Let us now also update the frontend to show an input field and a box where we print out the info returned by the LLM:
  • And a little bit of Javascript to call out our chat api and process the response:

That’s it!

More information

Microsoft.Extensions.AI libraries - .NET | Microsoft Learn

Popular posts from this blog

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

Kubernetes–Limit your environmental impact

Reducing the carbon footprint and CO2 emission of our (cloud) workloads, is a responsibility of all of us. If you are running a Kubernetes cluster, have a look at Kube-Green . kube-green is a simple Kubernetes operator that automatically shuts down (some of) your pods when you don't need them. A single pod produces about 11 Kg CO2eq per year( here the calculation). Reason enough to give it a try! Installing kube-green in your cluster The easiest way to install the operator in your cluster is through kubectl. We first need to install a cert-manager: kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.5/cert-manager.yaml Remark: Wait a minute before you continue as it can take some time before the cert-manager is up & running inside your cluster. Now we can install the kube-green operator: kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml Now in the namespace where we want t...

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...