Add startup filter to set MiddlewareFilterBuilder.ApplicationBuilder

This commit is contained in:
James Newton-King 2018-07-13 12:23:30 +12:00 committed by GitHub
parent a5083d525b
commit f12f9b46ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 7 deletions

View File

@ -79,9 +79,6 @@ namespace Microsoft.AspNetCore.Builder
VerifyMvcIsRegistered(app);
var middlewarePipelineBuilder = app.ApplicationServices.GetRequiredService<MiddlewareFilterBuilder>();
middlewarePipelineBuilder.ApplicationBuilder = app.New();
var routes = new RouteBuilder(app)
{
DefaultHandler = app.ApplicationServices.GetRequiredService<MvcRouteHandler>(),

View File

@ -273,6 +273,8 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddSingleton<MiddlewareFilterConfigurationProvider>();
// This maintains a cache of middleware pipelines, so it needs to be a singleton
services.TryAddSingleton<MiddlewareFilterBuilder>();
// Sets ApplicationBuilder on MiddlewareFilterBuilder
services.TryAddEnumerable(ServiceDescriptor.Singleton<IStartupFilter, MiddlewareFilterBuilderStartupFilter>());
}
private static void ConfigureDefaultServices(IServiceCollection services)

View File

@ -0,0 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Mvc.Internal
{
internal class MiddlewareFilterBuilderStartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return MiddlewareFilterBuilder;
void MiddlewareFilterBuilder(IApplicationBuilder builder)
{
var middlewarePipelineBuilder = builder.ApplicationServices.GetRequiredService<MiddlewareFilterBuilder>();
middlewarePipelineBuilder.ApplicationBuilder = builder.New();
next(builder);
}
}
}
}

View File

@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Mvc
var services = new ServiceCollection();
// Register a mock implementation of each service, AddMvcServices should add another implementation.
foreach (var serviceType in MutliRegistrationServiceTypes)
foreach (var serviceType in MultiRegistrationServiceTypes)
{
var mockType = typeof(Mock<>).MakeGenericType(serviceType.Key);
services.Add(ServiceDescriptor.Transient(serviceType.Key, mockType));
@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.Mvc
MvcCoreServiceCollectionExtensions.AddMvcCoreServices(services);
// Assert
foreach (var serviceType in MutliRegistrationServiceTypes)
foreach (var serviceType in MultiRegistrationServiceTypes)
{
AssertServiceCountEquals(services, serviceType.Key, serviceType.Value.Length + 1);
@ -222,14 +222,14 @@ namespace Microsoft.AspNetCore.Mvc
var services = new ServiceCollection();
MvcCoreServiceCollectionExtensions.AddMvcCoreServices(services);
var multiRegistrationServiceTypes = MutliRegistrationServiceTypes;
var multiRegistrationServiceTypes = MultiRegistrationServiceTypes;
return services
.Where(sd => !multiRegistrationServiceTypes.Keys.Contains(sd.ServiceType))
.Select(sd => sd.ServiceType);
}
}
private Dictionary<Type, Type[]> MutliRegistrationServiceTypes
private Dictionary<Type, Type[]> MultiRegistrationServiceTypes
{
get
{
@ -313,6 +313,13 @@ namespace Microsoft.AspNetCore.Mvc
typeof(MvcEndpointDataSource),
}
},
{
typeof(IStartupFilter),
new Type[]
{
typeof(MiddlewareFilterBuilderStartupFilter)
}
},
};
}
}