mirror of https://github.com/abpframework/abp.git
6 changed files with 101 additions and 10 deletions
@ -0,0 +1,47 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using Microsoft.AspNetCore.Routing; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
|
|||
namespace Microsoft.AspNetCore.Builder |
|||
{ |
|||
public static class AbpAspNetCoreApplicationBuilderExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Maps endpoints configured with the <see cref="AbpEndpointRouterOptions"/>.
|
|||
/// It internally uses the standard app.UseEndpoints(...) method.
|
|||
/// </summary>
|
|||
/// <param name="app">The application builder</param>
|
|||
/// <param name="additionalConfigurationAction">Additional (and optional) endpoint configuration</param>
|
|||
/// <returns></returns>
|
|||
public static IApplicationBuilder UseConfiguredEndpoints( |
|||
this IApplicationBuilder app, |
|||
Action<IEndpointRouteBuilder> additionalConfigurationAction = null) |
|||
{ |
|||
var options = app.ApplicationServices |
|||
.GetRequiredService<IOptions<AbpEndpointRouterOptions>>() |
|||
.Value; |
|||
|
|||
if (!options.EndpointConfigureActions.Any()) |
|||
{ |
|||
return app; |
|||
} |
|||
|
|||
return app.UseEndpoints(endpoints => |
|||
{ |
|||
using (var scope = app.ApplicationServices.CreateScope()) |
|||
{ |
|||
var context = new EndpointRouteBuilderContext(endpoints, scope.ServiceProvider); |
|||
|
|||
foreach (var configureAction in options.EndpointConfigureActions) |
|||
{ |
|||
configureAction(context); |
|||
} |
|||
|
|||
additionalConfigurationAction?.Invoke(endpoints); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Microsoft.AspNetCore.Routing |
|||
{ |
|||
public class AbpEndpointRouterOptions |
|||
{ |
|||
public List<Action<EndpointRouteBuilderContext>> EndpointConfigureActions { get; } |
|||
|
|||
public AbpEndpointRouterOptions() |
|||
{ |
|||
EndpointConfigureActions = new List<Action<EndpointRouteBuilderContext>>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System; |
|||
|
|||
namespace Microsoft.AspNetCore.Routing |
|||
{ |
|||
public class EndpointRouteBuilderContext |
|||
{ |
|||
public IEndpointRouteBuilder Endpoints { get; } |
|||
public IServiceProvider ScopeServiceProvider { get; } |
|||
|
|||
public EndpointRouteBuilderContext( |
|||
IEndpointRouteBuilder endpoints, |
|||
IServiceProvider scopeServiceProvider) |
|||
{ |
|||
Endpoints = endpoints; |
|||
ScopeServiceProvider = scopeServiceProvider; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue