During practice of book Pro ASP.NET Core by Adam Freeman, I have noticed the following usage of MapGet("route", RequestDelegate)
method :
app.MapGet("endpoint/function",
async (HttpContext context, IResponseFormatter formatter) => {
await formatter.Format(context, "Endpoint Function: It is sunny in LA");
});
IResponseFormatter
was registered in DI as singleton, but I am confused why request delegate with following source code
public delegate Task RequestDelegate(HttpContext context);
is accepting extra arguments, beyond of its definition.
Here I have found that it is possible, but when I tried to create delegate and function with extra parameters - it did not work.
So during search, I did not find any documentation or articles, when and how extra arguments work, in order to use it properly, please could you provide any articles or documentation or any explanation?
RequestDelegate, like all delegate types in .NET, does not accept extra arguments. Instead, the code compiles because the MapGet extension method has two overloads:
One of the overloads accepts a Delegate, the base class of all delegate types, which allows you to pass a method with arbitrary parameters.
In particular, when you pass the function
async (HttpContext context, IResponseFormatter formatter) => {
await formatter.Format(context, "Endpoint Function: It is sunny in LA");
}
to MapGet, the compiler automatically instantiates a Func<HttpContext, IResponseFormatter, Task>
delegate (not a RequestDelegate) that references the function. The Func is then implicitly converted to the Delegate base class.