Introduction
Not every AI application needs to wait for a user request before doing work. Many business processes happen in the background without direct user interaction.
Consider the following scenarios:
Generating daily business reports
Monitoring application logs
Summarizing support tickets every hour
Analyzing new documents as they arrive
Tracking infrastructure health
Sending AI-generated alerts
Processing customer feedback overnight
These workloads are ideal candidates for background workers and scheduled agents.
Traditionally, developers implemented these tasks using Windows Services, cron jobs, Azure Functions, or custom scheduling solutions. While these approaches work, managing distributed applications and coordinating AI-powered workloads can become challenging.
.NET Aspire simplifies the development of cloud-native distributed applications by providing tools for orchestration, service discovery, observability, and local development.
When combined with AI agents, .NET Aspire enables developers to build intelligent background services that operate continuously and reliably in production environments.
In this article, you'll learn how AI-powered background workers work, how .NET Aspire supports distributed applications, and how to build scheduled AI agents using .NET.
Understanding Background Workers
A background worker is a service that runs independently of user requests.
Instead of:
User Request
|
v
Application
|
v
Response
A background worker operates continuously.
Application
|
v
Background Service
|
v
Processing
The worker executes tasks automatically.
What Are Scheduled AI Agents?
Scheduled agents execute AI-powered tasks at predefined intervals.
Examples:
Every Hour
Every Day
Every Week
Workflow:
Schedule
|
v
Agent
|
v
AI Processing
|
v
Result
The agent operates without manual intervention.
Why Use AI-Powered Background Workers?
Many AI tasks are better suited for asynchronous execution.
Examples include:
Report Generation
Collect Data
|
v
Generate Summary
|
v
Send Report
Ticket Analysis
Support Tickets
|
v
AI Classification
|
v
Priority Assignment
Log Monitoring
Application Logs
|
v
AI Analysis
|
v
Alert Generation
These workloads benefit from background execution.
Understanding .NET Aspire
.NET Aspire is a cloud-ready stack for building distributed applications.
It provides:
Service orchestration
Service discovery
Distributed observability
Configuration management
Local development support
Aspire helps developers build and manage modern cloud-native systems.
Aspire Architecture
A typical Aspire solution looks like this:
App Host
|
+-- API
|
+-- Worker
|
+-- Database
|
+-- Cache
Each service becomes part of a distributed application.
Creating an Aspire Project
Create a new Aspire solution.
dotnet new aspire
This generates:
AppHost
ServiceDefaults
Application projects
The solution is ready for distributed development.
Creating a Worker Service
Add a worker project.
dotnet new worker -n AiWorker
A worker service provides the foundation for background processing.
Understanding BackgroundService
ASP.NET Core provides the BackgroundService class.
Example:
public class Worker
: BackgroundService
{
protected override async Task
ExecuteAsync(
CancellationToken token)
{
while (!token
.IsCancellationRequested)
{
await Task.Delay(
1000,
token);
}
}
}
This service runs continuously.
Creating an AI Agent Worker
A basic AI worker might look like this:
public class AiWorker
: BackgroundService
{
protected override async Task
ExecuteAsync(
CancellationToken token)
{
while (!token
.IsCancellationRequested)
{
Console.WriteLine(
"Running AI task");
await Task.Delay(
TimeSpan.FromMinutes(5),
token);
}
}
}
The worker executes tasks repeatedly.
Registering the Worker
Configure the service in Program.cs.
builder.Services
.AddHostedService<
AiWorker>();
The worker starts automatically when the application launches.
Creating Scheduled Jobs
Many workloads run on a schedule.
Example:
Every Day
9:00 AM
Workflow:
Scheduler
|
v
AI Agent
|
v
Result
Scheduling allows predictable execution.
Building a Daily Report Agent
Imagine a sales reporting system.
Workflow:
Sales Data
|
v
AI Analysis
|
v
Summary
|
v
Email
The report is generated automatically each day.
Using Semantic Kernel
Semantic Kernel can power AI workflows.
Workflow:
Data
|
v
Semantic Kernel
|
v
LLM
|
v
Insights
The worker invokes AI functionality as part of its execution process.
Example Report Generation
Input:
Sales increased by 18%.
Generated output:
Revenue growth was strong
this period with an 18%
increase in sales.
The report becomes more readable and actionable.
Processing Support Tickets
Scheduled agents can analyze incoming support requests.
Workflow:
New Tickets
|
v
AI Analysis
|
v
Priority Assignment
Output:
High Priority
This improves response times.
Join the conversation! Your thoughts help the community grow.