Custom base policy for OutputCache not triggered when API is called
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
I think this is the same issue as #45564.
When attempting to use a custom base policy for the OutputCache, the policy is not triggered or used when the API is called. Instead, a named policy must be created for caching to work correctly.
### Expected Behavior
The custom base policy should be triggered and used when making requests to the API.
### Steps To Reproduce
Add the custom policy to program.cs
```csharp
builder.Services.AddOutputCache(options =>
{
options.AddBasePolicy(builder => builder.AddPolicy(), true); //Doesn't work
options.AddBasePolicy(builder => builder.AddPolicy()); //Doesn't work
options.AddPolicy("PolicyA", builder => builder.AddPolicy(), true); //Works
});
```
The app builder:
```csharp
var app = builder.Build();
app.UseDeveloperExceptionPage();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
//This needs to be after Swagger, Middlewares and Authentication
app.UseOutputCache();
// app.MapControllers().CacheOutput().RequireAuthorization(); //Relying on the custom base policy doesn't work
app.MapControllers().CacheOutput("PolicyA").RequireAuthorization();
app.Run();
```
This is the CustomBasePolicy I used to test:
```csharp
public class CustomBasePolicy : IOutputCachePolicy
{
public ValueTask CacheRequestAsync(OutputCacheContext context, CancellationToken cancellation)
{
// always cache
context.EnableOutputCaching = true;
context.AllowCacheLookup = true;
context.AllowCacheStorage = true;
context.AllowLocking = true;
// Vary by any query by default
context.CacheVaryByRules.QueryKeys = "*";
return ValueTask.CompletedTask;
}
public ValueTask ServeFromCacheAsync(OutputCacheContext context, CancellationToken cancellation) => ValueTask.CompletedTask;
public ValueTask ServeResponseAsync(OutputCacheContext context, CancellationToken cancellation) => ValueTask.CompletedTask;
}
```
### Exceptions (if any)
_No response_
### .NET Version
7.0.200
### Anything else?
I tried to enable debug logs in my project and tried to look for specific logs that I found in the [OutputCacheMiddleware](https://github.com/dotnet/aspnetcore/blob/5d10dceeaf5d1d4ac945356f580cce5cf0486c40/src/Middleware/OutputCaching/src/OutputCacheMiddleware.cs#L228), however it never looks like the middleware was hit when I tried to use the base policy.
dotnet --info
```
.NET SDK:
Version: 7.0.200
Commit: 534117727b
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22621
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.200\
Host:
Version: 7.0.3
Architecture: x64
Commit: 0a2bda10e8
.NET SDKs installed:
7.0.103 [C:\Program Files\dotnet\sdk]
7.0.200 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Environment variables:
Not set
global.json file:
Not found
```
Contributor guide
Assessment
This issue has not been assessed yet.