How to Integrate Advanced AI Features in .NET Aspire Apps on Azure App Service
You can make and launch an intelligent AI app with .NET Aspire on Azure App Service. Azure AI Foundry Agent Service lets you add a chat feature to your app. When you link your intelligent AI app to the MCP agent ecosystem, it can do more things. Setting up the environment and connecting AI services helps you build better solutions.
Key Takeaways
First, make an Azure account. Then, install the newest .NET SDK. This gets you ready to make your smart AI app.
Add the Azure.AI.Foundry NuGet package to your app. This lets your app use chat and language tools.
Use Azure OpenAI Service to make your app smarter with words. This helps your app write text and answer questions well.
Watch how your app works with Application Insights. This tool shows you how healthy your app is and how people use it. You can fix problems fast.
Keep your app safe by putting API keys in Azure Key Vault. Use HTTPS to protect important data and keep it safe when sending.
Prerequisites and Setup
Azure and .NET Aspire Environment
You need to prepare your environment before you start building your intelligent AI app. First, make sure you have an active Azure account. If you do not have one, you can sign up for a free trial on the Azure website.
Next, install the latest .NET SDK. You can download it from the official .NET website. After installing the SDK, check your version by running this command in your terminal:
dotnet --version
You also need Visual Studio or Visual Studio Code. These tools help you write and manage your code. Choose the one you like best.
Tip: Use the Azure CLI to manage your resources. It saves time and helps you automate tasks.
App Service Configuration
Azure App Service lets you host your intelligent AI app in the cloud. You need to create a new App Service instance. Follow these steps:
Go to the Azure Portal.
Click on "Create a resource."
Select "Web App."
Fill in the required details, such as the app name and resource group.
Choose the runtime stack as ".NET."
Click "Review + create," then click "Create."
After deployment, you can configure your app settings. Set environment variables and connection strings in the "Configuration" section of your App Service. This step helps your app connect to Azure AI services and other resources.
Note: Always use strong passwords and secure your connection strings.
You have now set up the basic environment. You are ready to start building and deploying your intelligent AI app.
Build an Intelligent AI App
Azure AI Foundry Integration
You can build your intelligent AI app by linking it to the Azure AI Foundry Agent Service. This service helps you add smart things like chat and language understanding. First, add the Azure.AI.Foundry NuGet package to your .NET Aspire project. Use this command to do it:
dotnet add package Azure.AI.Foundry
Next, set up your connection to the Azure AI Foundry Agent Service. You need to put your service endpoint and API key in your app settings. In your appsettings.json
file, add this:
{
"FoundryAgent": {
"Endpoint": "https://<your-foundry-endpoint>.azure.com",
"ApiKey": "<your-api-key>"
}
}
Now, you can use the Foundry Agent client in your code. Here is a simple example in C#:
var agentClient = new FoundryAgentClient(
new Uri(Configuration["FoundryAgent:Endpoint"]),
Configuration["FoundryAgent:ApiKey"]
);
var response = await agentClient.GetResponseAsync("Hello, how can I help you?");
Console.WriteLine(response);
This setup lets your intelligent AI app talk to the Azure AI Foundry Agent Service. Now you can make features that understand and answer what users say.
Tip: Keep your API keys safe by storing them in Azure Key Vault.
Conversational Interface with Agents
You can make your intelligent AI app more useful by adding a way for users to chat with it. This means people can send messages and get answers right away. To do this, connect your .NET Aspire APIs to the Foundry Agent Service.
First, make an API endpoint in your app that gets user messages. Here is a simple example using ASP.NET Core:
[ApiController]
[Route("api/chat")]
public class ChatController : ControllerBase
{
private readonly FoundryAgentClient _agentClient;
public ChatController(FoundryAgentClient agentClient)
{
_agentClient = agentClient;
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] string userMessage)
{
var response = await _agentClient.GetResponseAsync(userMessage);
return Ok(response);
}
}
This code lets your app get messages and send them to the agent. The agent sends back a reply, and your app gives the answer to the user. You can use this to make chatbots, helpers, or support desks.
Note: Try asking your app different questions to make sure it works well.
MCP Agent Ecosystem
When you connect your intelligent AI app to the MCP agent ecosystem, your app can do even more. The Model Context Protocol (MCP) and AgentToAgent (A2A) standards help your app work with other agents and services. Here are some good things about it:
Agents can share information, so they work better together.
Your app can grow by letting agents help each other.
You can split big jobs into smaller parts, so your app works faster.
To join the MCP agent ecosystem, sign up your agent with the MCP registry. Change your app settings to add MCP endpoints and credentials. This lets your app talk to other agents and join bigger workflows.
Tip: Watch your agent’s activity and connections with the MCP dashboard.
By doing these steps, you can build an intelligent AI app that uses advanced Azure AI features, supports real-time chats, and works with other agents in a way that can grow.
Enhance with Azure OpenAI and Semantic Kernel
OpenAI Service Connection
You can link your .NET Aspire app to Azure OpenAI Service. This lets your app use advanced language features. First, make an Azure OpenAI resource in the Azure portal. After you create it, find your endpoint and API key. Put these values into your app settings. Here is an example:
{
"OpenAI": {
"Endpoint": "https://<your-openai-endpoint>.openai.azure.com",
"ApiKey": "<your-openai-api-key>"
}
}
Use the Azure.AI.OpenAI NuGet package to talk to the service. Install it like this:
dotnet add package Azure.AI.OpenAI
Now, you can send prompts and get answers from large language models. This helps your app understand and make text, answer questions, and give summaries.
Tip: Keep your API keys safe in Azure Key Vault.
RAG and Agentic Features
You can make your app smarter with Retrieval-Augmented Generation (RAG) and agentic features. These tools help your app find good information and give helpful answers. Here are some steps to follow:
Add vector search with SQL queries. This helps your app find the best answers.
Use orchestration frameworks like Semantic Kernel. This gives your app more control and options.
Add semantic caching. This makes your app faster by using fewer calls to large language models.
These steps help your intelligent AI app give fast and correct answers.
Semantic Kernel Setup
Semantic Kernel lets you build workflows that use AI skills and plugins. First, add the Microsoft.SemanticKernel NuGet package:
dotnet add package Microsoft.SemanticKernel
Set up the kernel in your code like this:
var kernel = new KernelBuilder()
.WithAzureOpenAIChatCompletionService(
endpoint: Configuration["OpenAI:Endpoint"],
apiKey: Configuration["OpenAI:ApiKey"])
.Build();
Now, you can add plugins or skills to your kernel. This setup helps your app plan, think, and act based on what users say. With Semantic Kernel, you can make custom workflows and connect to other services.
Note: Try different prompts to see how your app works.
Observability and Best Practices
Application Insights Monitoring
You want to see how your intelligent AI app is doing right now. Application Insights lets you check your app’s health and speed. First, add the Microsoft.ApplicationInsights.AspNetCore NuGet package to your project:
dotnet add package Microsoft.ApplicationInsights.AspNetCore
Next, put your Application Insights connection string in your app settings. You can find this string in the Azure portal. Add it like this:
{
"ApplicationInsights": {
"ConnectionString": "<your-connection-string>"
}
}
After you set it up, Application Insights gathers data about requests, errors, and how people use your app. You can look at charts and logs in the Azure portal. This helps you spot problems and fix them quickly.
Tip: Make custom events to watch special things your app does.
Security and Scaling
You need to keep your intelligent AI app safe. Always put secrets, like API keys, in Azure Key Vault. Set up role-based access control (RBAC) so only the right people can change your app. Use HTTPS to keep data safe as it moves.
Scaling helps your app work for more users. Azure App Service lets you scale up or out with just a few clicks. You can make rules to add more resources when you get more traffic. This keeps your app fast and working well.
DevOps Automation
DevOps helps you do your work faster. Set up a CI/CD pipeline with GitHub Actions or Azure DevOps. This pipeline builds, tests, and puts your app online by itself. You save time and make fewer mistakes.
Here is a simple GitHub Actions workflow for .NET:
name: Build and Deploy
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '8.0.x'
- name: Build
run: dotnet build --configuration Release
- name: Deploy to Azure
uses: azure/webapps-deploy@v2
with:
app-name: <your-app-name>
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}
Note: Test your pipeline often so you can find problems early.
You now know how to make and launch an intelligent AI app with .NET Aspire on Azure App Service. You got your setup ready, linked Azure AI Foundry and OpenAI, and added Application Insights to watch your app.
Try out different features and see what your app does.
Look for more guides to learn new things.
Try adding new tools to help your app get smarter.
FAQ
How do you secure API keys in your .NET Aspire app?
You should store API keys in Azure Key Vault. This keeps your secrets safe. Update your app settings to pull keys from Key Vault. Never put keys directly in your code.
Tip: Use managed identities for easy and secure access to Key Vault.
Can you scale your AI app automatically on Azure App Service?
Yes, you can set up autoscale rules in Azure. These rules add or remove resources based on your app’s needs. This helps your app handle more users without slowing down.
What is the best way to monitor your app’s health?
You can use Application Insights. It tracks requests, errors, and user actions. You can view charts and logs in the Azure portal. This helps you spot and fix problems quickly.
How do you connect your .NET Aspire app to Azure OpenAI Service?
First, install the Azure.AI.OpenAI NuGet package. Add your endpoint and API key to your app settings. Then, use the OpenAI client in your code to send prompts and get responses.
var client = new OpenAIClient(endpoint, new AzureKeyCredential(apiKey));