Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
| Value | |
|---|---|
| Rule ID | RDG002 |
| Fix is breaking or non-breaking | Non-breaking |
Cause
This diagnostic is emitted by the Request Delegate Generator when an endpoint contains a route handler that can't be statically analyzed.
Rule description
The Request Delegate Generator runs at compile-time and needs to be able to statically analyze route handlers in an app. The current implementation only supports route handlers that are provided as a lambda expression, method group references, or references to read-only fields or variables.
The following code generates the RDG002 warning because the route handler is provided as a reference to a method:
using System.Text.Json.Serialization;
var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0,
AppJsonSerializerContext.Default);
});
var app = builder.Build();
var del = Wrapper.GetTodos;
app.MapGet("/v1/todos", del);
app.Run();
record Todo(int Id, string Task);
[JsonSerializable(typeof(Todo[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
class Wrapper
{
public static Func<IResult> GetTodos = () =>
Results.Ok(new Todo(1, "Write test fix"));
}
How to fix violations
Declare the route handler using supported syntax, such as an inline lambda:
using System.Text.Json.Serialization;
var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0,
AppJsonSerializerContext.Default);
});
var app = builder.Build();
app.MapGet("/v1/todos", () => Results.Ok(new Todo(1, "Write tests")));
app.Run();
record Todo(int Id, string Task);
[JsonSerializable(typeof(Todo[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
When to suppress warnings
This warning can be safely suppressed. When suppressed, the framework falls back to generating the request delegate at runtime.
ASP.NET Core