public static IEndpointConventionBuilder MapNathan(this IEndpointRouteBuilder endpointRouteBuilder, Action <INathanApplicationConfigurator> configuration)
        {
            var applicationServiceProvider = endpointRouteBuilder
                                             .CreateApplicationBuilder()
                                             .ApplicationServices;
            var nathanApplicationConfigurator = new NathanApplicationConfigurator(applicationServiceProvider);

            nathanApplicationConfigurator
            .WithMiddleware <HandlerDispatchingMiddleware>(NathanPipelineComponents.HandlerDispatching);

            var nathanFeatureProvider = applicationServiceProvider
                                        .GetRequiredService <INathanFeatureProvider>();
            var nathanFeatures = nathanFeatureProvider.GetFeatures();

            foreach (var nathanFeature in nathanFeatures)
            {
                nathanFeature.Configure(nathanApplicationConfigurator);
            }

            configuration(nathanApplicationConfigurator);

            var nathanApplicationBuilder       = new NathanApplicationBuilder(applicationServiceProvider);
            var nathanApplicationConfiguration = nathanApplicationConfigurator.Configure();
            var nathanRequestDelegate          = nathanApplicationBuilder
                                                 .Build(nathanApplicationConfiguration);

            var nathanModuleProvider = applicationServiceProvider
                                       .GetService <INathanModuleProvider>();
            var modules = nathanModuleProvider.GetModules();
            var endPointConventionBuilders = new List <IEndpointConventionBuilder>();

            foreach (var module in modules)
            {
                var handlerDescriptors = module.ModuleDescriptor.HandlerDescriptors.Values;
                foreach (var handlerDescriptor in handlerDescriptors)
                {
                    var moduleDescriptor          = handlerDescriptor.ParentModuleDescriptor;
                    var pathTemplate              = handlerDescriptor.PathTemplate;
                    var methods                   = new [] { handlerDescriptor.Method };
                    var endPointConventionBuilder = endpointRouteBuilder.MapMethods(pathTemplate, methods, async httpContext =>
                    {
                        httpContext.Items.Add(NathanObjectKeys.HandlerDescriptor, handlerDescriptor);
                        var nathanRequestContext = new NathanRequestContext(httpContext);
                        await nathanRequestDelegate.Invoke(nathanRequestContext);
                    });
                    endPointConventionBuilder.WithMetadata(moduleDescriptor.MetaData);
                    handlerDescriptor.EndPointConventionBuilder.Execute(endPointConventionBuilder);
                    endPointConventionBuilder.WithMetadata(handlerDescriptor);
                    endPointConventionBuilders.Add(endPointConventionBuilder);
                }
            }
            return(new AggregatedEndpointConventionBuilder(endPointConventionBuilders));
        }
        public async Task Invoke(NathanRequestContext nathanRequestContext, NathanRequestDelegate next)
        {
            var httpContext             = nathanRequestContext.HttpContext;
            var storedHandlerDescriptor = nathanRequestContext.HandlerDescriptor;
            var nathanModule            = httpContext
                                          .RequestServices
                                          .GetRequiredService(storedHandlerDescriptor.ParentModuleDescriptor.ModuleType) as NathanModule;
            var nathanHandler = nathanModule
                                .ModuleDescriptor
                                .HandlerDescriptors
                                .First(d => d.Key == storedHandlerDescriptor.Key)
                                .Value
                                .RequestDelegate;

            await nathanHandler(nathanRequestContext);
        }
Beispiel #3
0
 public async Task Invoke(NathanRequestContext nathanRequestContext, NathanRequestDelegate next)
 {
     await next(nathanRequestContext);
 }