Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding IStartupFilter support #1675

Merged
merged 1 commit into from
Apr 19, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder.Internal;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -37,7 +38,6 @@ public async Task Invoke(HttpContext httpContext)
_logger.LogInformation("Begin Routing Request");
}


var shellSettings = httpContext.Features.Get<ShellSettings>();

// Define a PathBase for the current request that is the RequestUrlPrefix.
Expand Down Expand Up @@ -85,15 +85,32 @@ public async Task Invoke(HttpContext httpContext)
// Build the middleware pipeline for the current tenant
public RequestDelegate BuildTenantPipeline(ShellSettings shellSettings, IServiceProvider serviceProvider)
{
var startups = serviceProvider.GetServices<IStartup>();
var appBuilder = new ApplicationBuilder(serviceProvider);

// Create a nested pipeline to configure the tenant middleware pipeline
var startupFilters = serviceProvider.GetService<IEnumerable<IStartupFilter>>();
Action<IApplicationBuilder> configure = ConfigureTenantPipeline;
foreach (var filter in startupFilters.Reverse())
{
configure = filter.Configure(configure);
}

configure(appBuilder);

var pipeline = appBuilder.Build();

return pipeline;
}

private void ConfigureTenantPipeline(IApplicationBuilder builder)
{
var startups = builder.ApplicationServices.GetServices<IStartup>();

// IStartup instances are ordered by module dependency with an Order of 0 by default.
// OrderBy performs a stable sort so order is preserved among equal Order values.
startups = startups.OrderBy(s => s.Order);

var tenantRouteBuilder = serviceProvider.GetService<IModularTenantRouteBuilder>();

var appBuilder = new ApplicationBuilder(serviceProvider);
var tenantRouteBuilder = builder.ApplicationServices.GetService<IModularTenantRouteBuilder>();
var routeBuilder = tenantRouteBuilder.Build();

// In the case of several tenants, they will all be checked by ShellSettings. To optimize
Expand All @@ -102,18 +119,14 @@ public RequestDelegate BuildTenantPipeline(ShellSettings shellSettings, IService
// And the ShellSettings test in TenantRoute would also be useless.
foreach (var startup in startups)
{
startup.Configure(appBuilder, routeBuilder, serviceProvider);
startup.Configure(builder, routeBuilder, builder.ApplicationServices);
}

tenantRouteBuilder.Configure(routeBuilder);

var router = routeBuilder.Build();

appBuilder.UseRouter(router);

var pipeline = appBuilder.Build();

return pipeline;
builder.UseRouter(router);
}
}
}