Last active
June 26, 2023 07:12
-
-
Save lakeman/7b3ed1fce8f216a86b9f37c004b74ac1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EndpointInit | |
{ | |
private readonly IServiceProvider provider; | |
private readonly IActionDescriptorCollectionProvider collectionProvider; | |
private readonly IActionInvokerFactory invokerFactory; | |
private readonly PageLoader pageLoader; | |
public EndpointInit(IServiceProvider provider, IActionDescriptorCollectionProvider collectionProvider, IActionInvokerFactory invokerFactory, PageLoader pageLoader) | |
{ | |
this.provider = provider; | |
this.collectionProvider = collectionProvider; | |
this.invokerFactory = invokerFactory; | |
this.pageLoader = pageLoader; | |
} | |
public async Task PreCompileAsync() | |
{ | |
var actions = collectionProvider.ActionDescriptors; | |
using var scope = provider.CreateScope(); | |
var httpContext = new DefaultHttpContext() | |
{ | |
RequestServices = scope.ServiceProvider | |
}; | |
foreach (var a in actions.Items) | |
{ | |
// roughly based on seeing this in the stack trace from a bind provider; | |
// https://github.com/dotnet/aspnetcore/blob/v5.0.3/src/Mvc/Mvc.Core/src/Routing/ActionEndpointFactory.cs#L420 | |
var route = new RouteData(); | |
foreach (var pair in a.RouteValues) | |
route.Values.Add(pair.Key, pair.Value); | |
var descriptor = a; | |
if (descriptor is PageActionDescriptor page) | |
descriptor = await pageLoader.LoadAsync(page); | |
var context = new ActionContext(httpContext, route, descriptor); | |
invokerFactory.CreateInvoker(context); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment