The core as an ASP.NET Core minimal API
The third way gives the core an HTTP address. The web SDK is one project file and one program, and the program is the smallest ASP.NET Core application there is: two routes, a body read and a text answer. The core does not know that it is behind HTTP, which is the point of the whole group.
The web project
The one difference from the console hosts is the SDK: Microsoft.NET.Sdk.Web brings the ASP.NET Core shared framework, so Kestrel and the routing are available without a package. The reference to the core is the same:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>
</Project>
The program
Two endpoints: a health route that answers ok and a POST that reads the body as the request line and answers with the text of the core. The program runs on a fixed port, which a real service takes from configuration:
using Core;
Console.WriteLine("api 1.0");
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/health", () => Results.Text("ok"));
app.MapPost("/price", async (HttpRequest request) =>
{
using var reader = new StreamReader(request.Body);
var line = await reader.ReadToEndAsync();
return Results.Text(Pricing.Process(line.Trim()), "text/plain");
});
app.Run("http://127.0.0.1:5099");
The start
The program was built in Release and started with dotnet Api.dll. Its own log names the address and the environment, which is everything a caller needs to know:
dotnet src/Api/bin/Release/net10.0/Api.dll
echo "status: $?"
api 1.0
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://127.0.0.1:5099
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /tmp/r12/project
The call
The caller is Python's urllib, the same library the service page used. The body is the line, the answer is the line the core printed, and the content type is plain text:
#!/usr/bin/env python3
"""Send one request line to the minimal API and print the answer."""
import sys
import urllib.request
line = sys.argv[1] if len(sys.argv) > 1 else "sku=pen qty=2 unit=1.50"
request = urllib.request.Request(
"http://127.0.0.1:5099/price", data=line.encode(), method="POST"
)
with urllib.request.urlopen(request) as response:
print(response.read().decode())
python3 python/post_api.py
python3 python/post_api.py "sku=pen qty=0 unit=1.50"
ok 3.00 0.60 3.60
err qty must be 1 to 100
- The API is the slowest of the three ways per call in absolute terms for a single client: 1.798 milliseconds against 0.023 for the daemon, because every call is an HTTP request with a connection and a parse. It is the fastest way to reach the core from another machine, and the only one of the three that a browser can call.
- The API pays its runtime start once, like the daemon: the timing page measured the calls after the server was listening.
- A production version adds authentication, a request id, logging and a container, and none of that changes the core. The next page measures the price of the three designs.
The next page is the measurement: the same call through a new process, through the daemon and through this API.
Exercises
- Add a GET route that takes the line as a query string and returns the same answer.
- Return status 400 when the answer starts with err and read it with urllib.
- Run two clients at once and see whether Kestrel handles them in parallel.
- Take the port from an environment variable and start two instances.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does Microsoft.NET.Sdk.Web add?
Answer
The ASP.NET Core shared framework: Kestrel and routing without a package
The project file names the SDK and references the core.
-
Which call maps the POST route?
Answer
app.MapPost
The program maps /price with MapPost.
-
What does this program print?
Console.WriteLine("ok 3.00 0.60 3.60");Answer
ok 3.00 0.60 3.60
That is the answer of the API to the valid line.
-
What did the start log name?
Answer
The address and the environment
The measured log has Now listening and Hosting environment.
-
What does this program print?
Console.WriteLine("ok");Answer
ok
The health route of the API answers ok.
-
Which way is the fastest per call?
Answer
The daemon, at about 0.02 milliseconds
The timing page measured all three in this container.
-
What does this program print?
Console.WriteLine(1.798 > 0.023);Answer
True
An HTTP answer of 1.798 ms is slower than the daemon per call.
-
Why does the core not know about HTTP?
Answer
The API program reads the body and calls the core, which returns a string
The separation is the subject of the group.
-
Which caller did the page use?
Answer
Python urllib
The page runs a Python file with urllib.
-
What would a production API add?
Answer
Authentication, a request id, logging and a container
None of it changes the core.
The C# code on this page was built with the .NET SDK 10.0.303 and the Python code was run with Python 3.14.10, on Alpine Linux 3.24.3 (x86_64, four processors) in a Docker container.
Article author: Arthur Isaev