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 next page is the measurement: the same call through a new process, through the daemon and through this API.

Exercises

  1. Add a GET route that takes the line as a query string and returns the same answer.
  2. Return status 400 when the answer starts with err and read it with urllib.
  3. Run two clients at once and see whether Kestrel handles them in parallel.
  4. 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.

  1. 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.

  2. Which call maps the POST route?

    Answer

    app.MapPost

    The program maps /price with MapPost.

  3. 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.

  4. What did the start log name?

    Answer

    The address and the environment

    The measured log has Now listening and Hosting environment.

  5. What does this program print? Console.WriteLine("ok");

    Answer

    ok

    The health route of the API answers ok.

  6. Which way is the fastest per call?

    Answer

    The daemon, at about 0.02 milliseconds

    The timing page measured all three in this container.

  7. 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.

  8. 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.

  9. Which caller did the page use?

    Answer

    Python urllib

    The page runs a Python file with urllib.

  10. 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