C
C#4mo ago
Anton

Eagerly compile minimal apis

I want to initialize minimal apis on initialization rather than the first request. Is there a way to manually force it to initialize? Other than making a request to one of the endpoints from within the application.
4 Replies
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
SleepWellPupper
SleepWellPupper4mo ago
A minimal api is a delegate, there's nothing to initialize? The instance is created when you map it.
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Anton
AntonOP4mo ago
So it seems to resolve each endpoint separately. I wanted to check if they could be called. For example, if it will be able to resolve the services injected into the constructor. The way I figured is calling the endpoint manually. Kind of like this:
app.UseEndpoints(x => {});

using (var scope = app.Services.CreateScope())
{
var httpContext = new DefaultHttpContext
{
RequestServices = scope.ServiceProvider,
};

var endpoint = GetEndpoint(httpContext);
try
{
await endpoint.RequestDelegate!(httpContext);
}
#pragma warning disable CA1031
// ReSharper disable once EmptyGeneralCatchClause
catch
#pragma warning restore CA1031
{
}

Endpoint GetEndpoint(HttpContext httpContext)
{
var endpointDataSource = httpContext.RequestServices.GetRequiredService<EndpointDataSource>();
var endpoints = endpointDataSource.Endpoints.OfType<RouteEndpoint>();
return endpoints.First();
}
}
app.UseEndpoints(x => {});

using (var scope = app.Services.CreateScope())
{
var httpContext = new DefaultHttpContext
{
RequestServices = scope.ServiceProvider,
};

var endpoint = GetEndpoint(httpContext);
try
{
await endpoint.RequestDelegate!(httpContext);
}
#pragma warning disable CA1031
// ReSharper disable once EmptyGeneralCatchClause
catch
#pragma warning restore CA1031
{
}

Endpoint GetEndpoint(HttpContext httpContext)
{
var endpointDataSource = httpContext.RequestServices.GetRequiredService<EndpointDataSource>();
var endpoints = endpointDataSource.Endpoints.OfType<RouteEndpoint>();
return endpoints.First();
}
}
But again, this doesn't seem to compile the whole graph.
Want results from more Discord servers?
Add your server