From 6c934e53e6f1773a8782ae4084b3c38e69fcfc88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 9 May 2020 01:08:52 +0300 Subject: [PATCH] Resolved #3880: Obsolete app.UseMvcWithDefaultRouteAndArea() and introduce app.UseConfiguredEndpoints(). --- ...pNetCoreMvcApplicationBuilderExtensions.cs | 10 +--- .../AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs | 12 +++++ ...tCoreMvcConventionalDependencyRegistrar.cs | 9 +++- ...pAspNetCoreApplicationBuilderExtensions.cs | 47 +++++++++++++++++++ .../Routing/AbpEndpointRouterOptions.cs | 15 ++++++ .../Routing/EndpointRouteBuilderContext.cs | 18 +++++++ 6 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpAspNetCoreApplicationBuilderExtensions.cs create mode 100644 framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Routing/AbpEndpointRouterOptions.cs create mode 100644 framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Routing/EndpointRouteBuilderContext.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Microsoft/AspNetCore/Builder/AbpAspNetCoreMvcApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Microsoft/AspNetCore/Builder/AbpAspNetCoreMvcApplicationBuilderExtensions.cs index 9a19337c5f..daa5183709 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Microsoft/AspNetCore/Builder/AbpAspNetCoreMvcApplicationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Microsoft/AspNetCore/Builder/AbpAspNetCoreMvcApplicationBuilderExtensions.cs @@ -15,18 +15,12 @@ namespace Microsoft.AspNetCore.Builder /// The . /// Additional action to configure routes /// A reference to this instance after the operation has completed. + [Obsolete("Use app.UseConfiguredEndpoints(...) extension method instead!")] public static IApplicationBuilder UseMvcWithDefaultRouteAndArea( this IApplicationBuilder app, Action additionalConfigurationAction = null) { - return app.UseEndpoints(endpoints => - { - endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}"); - endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); - endpoints.MapRazorPages(); - - additionalConfigurationAction?.Invoke(endpoints); - }); + return app.UseConfiguredEndpoints(additionalConfigurationAction); } } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs index 00ba8423b3..ee81e0a22e 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs @@ -13,10 +13,12 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Reflection; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Localization; using Volo.Abp.ApiVersioning; @@ -165,6 +167,16 @@ namespace Volo.Abp.AspNetCore.Mvc { mvcOptions.AddAbp(context.Services); }); + + Configure(options => + { + options.EndpointConfigureActions.Add(context => + { + context.Endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}"); + context.Endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); + context.Endpoints.MapRazorPages(); + }); + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalDependencyRegistrar.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalDependencyRegistrar.cs index 526470b3ad..2ef536db4d 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalDependencyRegistrar.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalDependencyRegistrar.cs @@ -29,8 +29,13 @@ namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection foreach (var serviceType in serviceTypes) { - var serviceDescriptor = ServiceDescriptor.Describe(serviceType, type, lifeTime); - services.Add(serviceDescriptor); + services.Add( + ServiceDescriptor.Describe( + serviceType, + type, + lifeTime + ) + ); } } diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpAspNetCoreApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpAspNetCoreApplicationBuilderExtensions.cs new file mode 100644 index 0000000000..1ce443c37f --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpAspNetCoreApplicationBuilderExtensions.cs @@ -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 + { + /// + /// Maps endpoints configured with the . + /// It internally uses the standard app.UseEndpoints(...) method. + /// + /// The application builder + /// Additional (and optional) endpoint configuration + /// + public static IApplicationBuilder UseConfiguredEndpoints( + this IApplicationBuilder app, + Action additionalConfigurationAction = null) + { + var options = app.ApplicationServices + .GetRequiredService>() + .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); + } + }); + } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Routing/AbpEndpointRouterOptions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Routing/AbpEndpointRouterOptions.cs new file mode 100644 index 0000000000..85c81a7949 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Routing/AbpEndpointRouterOptions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace Microsoft.AspNetCore.Routing +{ + public class AbpEndpointRouterOptions + { + public List> EndpointConfigureActions { get; } + + public AbpEndpointRouterOptions() + { + EndpointConfigureActions = new List>(); + } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Routing/EndpointRouteBuilderContext.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Routing/EndpointRouteBuilderContext.cs new file mode 100644 index 0000000000..80f25d2d38 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Routing/EndpointRouteBuilderContext.cs @@ -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; + } + } +} \ No newline at end of file