All jobs

Add Output Caching to an ASP.NET Core Minimal API

Yes. An agent adds the output cache middleware, a per-user policy for authorized endpoints, tag-based eviction on writes and functional tests, in a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads how requests reach the endpoints before it caches any. In a solution like jasontaylordev/CleanArchitecture, global.json pins the .NET 10 SDK, src/Web/Program.cs builds the app with AddServiceDefaults, AddWebServices and AddInfrastructureServices, then calls UseHttpsRedirection, UseCors, UseFileServer, MapOpenApi and MapEndpoints(typeof(Program).Assembly). src/Web/Infrastructure/WebApplicationExtensions.cs discovers every IEndpointGroup and mounts it under /api/{ClassName}, so src/Web/Endpoints/WeatherForecasts.cs serves GET /api/WeatherForecasts and src/Web/Endpoints/TodoLists.cs serves GET /api/TodoLists with POST, PUT and DELETE beside it. Every group calls RequireAuthorization() on its RouteGroupBuilder. src/Web/DependencyInjection.cs registers IUser as CurrentUser, which reads the NameIdentifier claim, and Directory.Packages.props already lists Microsoft.AspNetCore.OpenApi and the Aspire packages; output caching ships in the shared framework, so no package is added.

It wires the middleware in the order the framework wants. builder.Services.AddOutputCache(...) goes into AddWebServices, and app.UseOutputCache() goes into Program.cs after app.UseCors(...), because the output cache serves a stored response before CORS would run if the order were reversed. The default policy refuses to cache a request that carries an Authorization header or an authenticated user, and every endpoint here requires one, so the agent writes an IOutputCachePolicy in src/Web/Infrastructure/ that allows lookup and storage for authenticated GET requests, varies the cache key by the user id from IUser, and is registered under a name such as PerUser. GetWeatherForecasts gets .CacheOutput("PerUser") with a short Expire; the handler in GetWeatherForecastsQuery.cs builds its five entries from Random, so a second call returning the same body is the visible proof that the cache answered.

Writes evict what they change. GetTodoLists is tagged, and CreateTodoList, UpdateTodoList, DeleteTodoList and the three TodoItems commands resolve IOutputCacheStore and call EvictByTagAsync after the command succeeds, so a list never shows a stale item for longer than one request. The tag carries the user id so one user's write does not flush another user's entries. src/Web/Web.http, which already holds a signed-in GET /api/WeatherForecasts, gets a second identical request under it so a developer can watch the Age header appear.

Then it proves the behaviour. tests/Application.FunctionalTests drives the app through TestApp.SendAsync with a mocked IUser from WebApiFactory.cs, so the agent adds a test fixture that registers a test authentication handler in ConfigureTestServices and calls the endpoints over CreateClient(): two GET /api/WeatherForecasts calls return the same body, a POST /api/TodoLists followed by GET /api/TodoLists returns the new list, and a second user does not see the first user's cached body. It runs dotnet build --configuration Release and dotnet test as .github/workflows/build.yml does, and reads the output before opening the pull request.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add output caching to this ASP.NET Core minimal API. Read global.json, src/Web/Program.cs, src/Web/DependencyInjection.cs, src/Web/Infrastructure/WebApplicationExtensions.cs, src/Web/Infrastructure/IEndpointGroup.cs, every file in src/Web/Endpoints, src/Web/Services/CurrentUser.cs, src/Application/WeatherForecasts/Queries/GetWeatherForecasts/GetWeatherForecastsQuery.cs, tests/Application.FunctionalTests/Infrastructure/WebApiFactory.cs and .github/workflows/build.yml first. Register AddOutputCache in AddWebServices and UseOutputCache in Program.cs after UseCors. Every endpoint group requires authorization, so write an IOutputCachePolicy that caches authenticated GET requests and varies by the current user id, and apply it to GET /api/WeatherForecasts with a short expiry and to GET /api/TodoLists with a per-user tag. Evict that tag from IOutputCacheStore in the TodoLists and TodoItems create, update and delete endpoints. Add functional tests that call the endpoints over HTTP with a test authentication handler: a repeated GET returns the same body, a write is visible on the next GET, and one user never receives another user's cached response. Run dotnet build and dotnet test, then open a pull request that lists each cached endpoint, its policy and what evicts it.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request that edits src/Web/Program.cs, src/Web/DependencyInjection.cs, src/Web/Endpoints/WeatherForecasts.cs, src/Web/Endpoints/TodoLists.cs, src/Web/Endpoints/TodoItems.cs and src/Web/Web.http, adds the policy class under src/Web/Infrastructure/, and adds a functional test class with its authentication handler. The description holds a table of cached endpoints with their expiry, their vary key and the tag that evicts them, and names the middleware order it chose and why.

The run's record keeps the dotnet build and dotnet test output with each test named, so a reviewer can see the cache tests among the existing TodoLists and TodoItems tests rather than take the description's word for it.

Before merging, a reviewer checks that no endpoint returning per-user data is cached without the user id in its key, that the eviction tag is called on every write path including UpdateTodoItemDetail, and that UseOutputCache still sits after UseCors and before MapEndpoints.

Run it on every endpoint group that is read far more than it is written

The setup is one POST /o/{org}/sessions with this body and the repository in resources, sent from a terminal; the run keeps going in its sandbox after the window closes, and a follow-up through POST /o/{org}/sessions/{id}/messages can ask for a Redis-backed store or a longer expiry without a new run. A team with several services sends one request per repository, each with its own UUID as request_id, so a script that is rerun returns the run it already started.

Give the API key a webhook_url and Rush POSTs run.completed or run.failed when a run's turn finishes, with session_url so your server can read the pull request link and the test output.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Caching an authorized API is where a quick change goes wrong quietly: the default policy skips authenticated requests, so a CacheOutput() dropped on an endpoint does nothing, and a custom policy that forgets the user in its key hands one user another user's todo lists. The agent runs the endpoints twice as two different users in its own sandbox and reads both responses, instead of reasoning about the policy from the diff.

The build and test commands run in the sandbox and their output stays with the run, so the pull request can point at the test that saw a stale list evicted and at the one that saw two users kept apart, which is what a reviewer would otherwise have to reproduce locally against a database.

Frequently asked questions

Why does the default output cache policy ignore my endpoints?

ASP.NET Core's base policy does not cache a request that carries an Authorization header or an authenticated user, and every endpoint group in this template calls RequireAuthorization(). The agent adds a named policy that opts those requests in and varies the cache key by the user id, so the cache works without weakening authorization.

Will one user ever see another user's cached response?

Not with the policy the agent writes: the user id from IUser is part of the cache key, and the eviction tag carries it too. A functional test signs in as two users and asserts the second never receives the first's body.

What happens when a todo list changes?

The create, update and delete endpoints for lists and items call EvictByTagAsync on IOutputCacheStore with that user's tag after the command succeeds, so the next GET /api/TodoLists runs the query again. Only the weather endpoint relies on expiry alone, since nothing in the app writes to it.

Can the cache be shared across instances?

Yes. The in-memory store is the default; swapping in Redis is a one-line registration of a distributed output cache store, and the Aspire app host in src/AppHost is where a Redis resource would be added. The agent leaves that as a note in the pull request rather than adding a service the repository does not run yet.

Does the middleware order matter?

Yes. UseOutputCache has to come after UseCors so a cached response still gets CORS headers, and before the endpoints are mapped. The agent places it there in Program.cs and says so in the description.

More .NET jobs

More jobs like this: Hand off a long task

Keep reading