Introduction
In this blog post, we will build a small Service-Oriented Architecture (SOA) application using .NET 9. The backend will provide a flight data service running on a three-node Kubernetes cluster in Microsoft Azure. Additionally, we will develop a simple Blazor frontend that consumes the flight data and deploy it to a separate cluster node. We will cover:
- Implementing the flight data service in .NET 9
- Deploying the service to Azure Kubernetes Service (AKS)
- Creating a Blazor frontend for displaying flight data
- Deploying the Blazor frontend to AKS
Step 1: Implement the Flight Data Service
We begin by creating an ASP.NET Core Web API that provides flight data.
Create the API Project
mkdir FlightService && cd FlightService<br> dotnet new webapi -n FlightService<br> cd FlightService
Define the Flight Data Model
public class Flight
{
public string FlightNumber { get; set; }
public string Origin { get; set; }
public string Destination { get; set; }
public DateTime DepartureTime { get; set; }
public DateTime ArrivalTime { get; set; }
}
Implement the Flight Service Controller
[ApiController]
[Route("api/[controller]")]
public class FlightsController : ControllerBase
{
private static readonly List<Flight> Flights = new()
{
new Flight { FlightNumber = "LH123", Origin = "MUC", Destination = "JFK", DepartureTime = DateTime.UtcNow.AddHours(2), ArrivalTime = DateTime.UtcNow.AddHours(8) },
new Flight { FlightNumber = "BA456", Origin = "LHR", Destination = "DXB", DepartureTime = DateTime.UtcNow.AddHours(3), ArrivalTime = DateTime.UtcNow.AddHours(9) }
};
[HttpGet]
public ActionResult<IEnumerable<Flight>> GetFlights()
{
return Ok(Flights);
}
}
Step 2: Deploy the Flight Service to Kubernetes in Azure
Create the Azure Kubernetes Service (AKS) Cluster
az aks create --resource-group MyResourceGroup --name MyAKSCluster --node-count 3 --generate-ssh-keys
Build and Push the Docker Image
docker build -t myacr.azurecr.io/flightservice:latest .
docker push myacr.azurecr.io/flightservice:latest
Create Kubernetes Deployment and Service
Create a deployment.yaml
file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: flightservice
spec:
replicas: 3
selector:
matchLabels:
app: flightservice
template:
metadata:
labels:
app: flightservice
spec:
containers:
- name: flightservice
image: myacr.azurecr.io/flightservice:latest
ports:
- containerPort: 80
Apply the deployment:
kubectl apply -f deployment.yaml
Step 3: Create the Blazor WebAssembly App
dotnet new blazorwasm -n FlightClient
cd FlightClient
Add Flight Data Service to Blazor
Modify Program.cs
:
builder.Services.AddHttpClient("FlightService", client =>
{
client.BaseAddress = new Uri("http://flightservice.default.svc.cluster.local");
});
Modify Index.razor
:
@inject HttpClient Http
<h3>Flight Data</h3>
@if (flights == null)
{
<p>Loading...</p>
}
else
{
<ul>
@foreach (var flight in flights)
{
<li>@flight.FlightNumber - @flight.Origin to @flight.Destination</li>
}
</ul>
}
@code {
private List<Flight> flights;
protected override async Task OnInitializedAsync()
{
flights = await Http.GetFromJsonAsync<List<Flight>>("api/flights");
}
}
Step 4: Deploy the Blazor App to Azure Kubernetes Service
Build and Push the Blazor Image
docker build -t myacr.azurecr.io/flightclient:latest .
docker push myacr.azurecr.io/flightclient:latest
Create Kubernetes Deployment for Blazor App
Create flightclient-deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: flightclient
spec:
replicas: 1
selector:
matchLabels:
app: flightclient
template:
metadata:
labels:
app: flightclient
spec:
containers:
- name: flightclient
image: myacr.azurecr.io/flightclient:latest
ports:
- containerPort: 80
Deploy it:
kubectl apply -f flightclient-deployment.yaml
Conclusion
In this post, we:
- Created a flight data service in .NET 9
- Deployed it to a three-node Azure Kubernetes cluster
- Built a Blazor WebAssembly frontend
- Deployed the frontend to another Kubernetes node
This architecture demonstrates scalability by running the backend across multiple nodes and hosting the frontend separately for better load distribution.