Add startup filter to set MiddlewareFilterBuilder.ApplicationBuilder
This commit is contained in:
parent
a5083d525b
commit
f12f9b46ed
|
|
@ -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>(),
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue