Introduction
In this guide, you’ll learn how to:
- Build a sample .NET 9 web API.
- Containerize it using Docker.
- Deploy it to an OpenShift cluster.
- Write a .NET 9 test client that calls the deployed API.
🎯 1. Create a .NET 9 Web API
Start by creating a simple RESTful service that returns product data.
dotnet new webapi -n ProductService
cd ProductService
📦 Product.cs
public record Product(int Id, string Name, decimal Price);
🧠 ProductsController.cs
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private static readonly List<Product> Products = new()
{
new Product(1, "Laptop", 1299.99m),
new Product(2, "Headphones", 199.99m)
};
[HttpGet]
public ActionResult<IEnumerable<Product>> Get() => Products;
[HttpGet("{id}")]
public ActionResult<Product> Get(int id)
{
var product = Products.FirstOrDefault(p => p.Id == id);
return product is null ? NotFound() : Ok(product);
}
}
🐳 2. Add a Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -o /app/publish
FROM base AS final
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "ProductService.dll"]
🧪 Test locally:
docker build -t productservice:1.0 .
docker run -p 5000:80 productservice:1.0
🌐 3. Deploy to OpenShift
Step 1: Push Image to Container Registry
If you’re using Docker Hub:
docker tag productservice:1.0 yourdockerhub/productservice:1.0
docker push yourdockerhub/productservice:1.0
Step 2: Login to OpenShift
oc login https://your-openshift-cluster --token=your-token
oc new-project demo-dotnet
Step 3: Deploy the App
oc new-app --name=productservice --docker-image=yourdockerhub/productservice:1.0
Step 4: Expose the Route
oc expose svc/productservice
You now get a public URL like:
https://productservice-demo-dotnet.apps.openshift.local
🤖 4. Create a .NET 9 Test Client
Let’s write a C# console app to query the deployed API.
📁 Create the Client
dotnet new console -n ProductClient
cd ProductClient
dotnet add package System.Net.Http.Json
📄 Program.cs
using System.Net.Http.Json;
var baseUrl = "https://productservice-demo-dotnet.apps.openshift.local/api/products";
using var client = new HttpClient();
// Get all products
var products = await client.GetFromJsonAsync<List<Product>>(baseUrl);
Console.WriteLine("Products:");
products?.ForEach(p => Console.WriteLine($"{p.Id}: {p.Name} - {p.Price:C}"));
// Get product by ID
var singleProduct = await client.GetFromJsonAsync<Product>($"{baseUrl}/1");
Console.WriteLine($"\nSingle Product:\n{singleProduct?.Id}: {singleProduct?.Name} - {singleProduct?.Price:C}");
record Product(int Id, string Name, decimal Price);
💡 You may need to trust the OpenShift TLS certificate or run with
--ignore-certificate-errorsin development.
🧪 5. Optional: Add Readiness & Liveness Probes
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 10
periodSeconds: 10
readinessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 5
periodSeconds: 10
You can implement a simple /health endpoint in your controller or use the built-in health checks middleware.
🎁 Summary
| Step | Tooling Used |
|---|---|
| Web API Development | .NET 9 Minimal API / WebAPI |
| Containerization | Docker |
| Deployment | OpenShift CLI (oc) |
| Public Exposure | OpenShift Route |
| Client Integration | .NET 9 HttpClient |
✅ Final Thoughts
OpenShift makes it easy to deploy and manage containerized applications at scale. Combining it with .NET 9’s powerful minimal APIs and streamlined performance, you can rapidly ship cloud-native apps that are testable, scalable, and observable.
With full CI/CD integrations (e.g. Tekton or GitOps), metrics dashboards, and autoscaling support, OpenShift is a great environment for hosting your .NET microservices.
Views: 53
