From 674291a9284e62e821a2128938c83ac9c6ee2207 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 13 Jun 2019 14:02:50 +0800 Subject: [PATCH] Resolve #1268 Implement IMiddleware for all asp.net core middlewares. --- .../AbpAspNetCoreMultiTenancyModule.cs | 2 ++ .../MultiTenancy/MultiTenancyMiddleware.cs | 10 +++----- .../Abp/AspNetCore/AbpAspNetCoreModule.cs | 8 +++++++ .../Auditing/AbpAuditingMiddleware.cs | 13 ++++------- .../AbpExceptionHandlingMiddleware.cs | 16 ++++++------- .../Tracing/AbpCorrelationIdMiddleware.cs | 23 +++++++++---------- .../AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs | 12 ++++------ .../Mvc/AbpAspNetCoreMvcTestModule.cs | 2 ++ .../FakeAuthenticationMiddleware.cs | 12 ++++------ 9 files changed, 48 insertions(+), 50 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyModule.cs b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyModule.cs index 64219b14d2..2fda5adc66 100644 --- a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyModule.cs @@ -19,6 +19,8 @@ namespace Volo.Abp.AspNetCore.MultiTenancy options.TenantResolvers.Add(new HeaderTenantResolveContributor()); options.TenantResolvers.Add(new CookieTenantResolveContributor()); }); + + context.Services.AddTransient(); } } } diff --git a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs index 417b0d30b3..10448839a4 100644 --- a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs @@ -5,30 +5,26 @@ using Volo.Abp.MultiTenancy; namespace Volo.Abp.AspNetCore.MultiTenancy { - public class MultiTenancyMiddleware + public class MultiTenancyMiddleware : IMiddleware { - private readonly RequestDelegate _next; - private readonly ITenantResolver _tenantResolver; private readonly ITenantStore _tenantStore; private readonly ICurrentTenant _currentTenant; private readonly ITenantResolveResultAccessor _tenantResolveResultAccessor; public MultiTenancyMiddleware( - RequestDelegate next, ITenantResolver tenantResolver, ITenantStore tenantStore, ICurrentTenant currentTenant, ITenantResolveResultAccessor tenantResolveResultAccessor) { - _next = next; _tenantResolver = tenantResolver; _tenantStore = tenantStore; _currentTenant = currentTenant; _tenantResolveResultAccessor = tenantResolveResultAccessor; } - public async Task Invoke(HttpContext httpContext) + public async Task InvokeAsync(HttpContext context, RequestDelegate next) { var resolveResult = _tenantResolver.ResolveTenantIdOrName(); _tenantResolveResultAccessor.Result = resolveResult; @@ -48,7 +44,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy using (_currentTenant.Change(tenant?.Id, tenant?.Name)) { - await _next(httpContext); + await next(context); } } diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs index f7cc0710e7..e8a0619ed0 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs @@ -1,6 +1,9 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AspNetCore.Auditing; +using Volo.Abp.AspNetCore.Mvc.ExceptionHandling; +using Volo.Abp.AspNetCore.Tracing; +using Volo.Abp.AspNetCore.Uow; using Volo.Abp.Auditing; using Volo.Abp.Authorization; using Volo.Abp.Domain; @@ -43,6 +46,11 @@ namespace Volo.Abp.AspNetCore AddAspNetServices(context.Services); context.Services.AddObjectAccessor(); + + context.Services.AddTransient(); + context.Services.AddTransient(); + context.Services.AddTransient(); + context.Services.AddTransient(); } private static void AddAspNetServices(IServiceCollection services) diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs index 8b1e63fd4d..c0b4238d65 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs @@ -7,32 +7,29 @@ using Volo.Abp.Users; namespace Volo.Abp.AspNetCore.Auditing { - public class AbpAuditingMiddleware + public class AbpAuditingMiddleware : IMiddleware { - private readonly RequestDelegate _next; private readonly IAuditingManager _auditingManager; protected AbpAuditingOptions Options { get; } protected ICurrentUser CurrentUser { get; } public AbpAuditingMiddleware( - RequestDelegate next, IAuditingManager auditingManager, ICurrentUser currentUser, IOptions options) { - _next = next; _auditingManager = auditingManager; CurrentUser = currentUser; Options = options.Value; } - public async Task Invoke(HttpContext httpContext) + public async Task InvokeAsync(HttpContext context, RequestDelegate next) { - if (!ShouldWriteAuditLog(httpContext)) + if (!ShouldWriteAuditLog(context)) { - await _next(httpContext); + await next(context); return; } @@ -40,7 +37,7 @@ namespace Volo.Abp.AspNetCore.Auditing { try { - await _next(httpContext); + await next(context); } finally { diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs index f36a7f4860..ce73f54cf2 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs @@ -10,41 +10,39 @@ using Volo.Abp.Json; namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling { - public class AbpExceptionHandlingMiddleware + public class AbpExceptionHandlingMiddleware : IMiddleware { - private readonly RequestDelegate _next; private readonly ILogger _logger; private readonly Func _clearCacheHeadersDelegate; - public AbpExceptionHandlingMiddleware(RequestDelegate next, ILogger logger) + public AbpExceptionHandlingMiddleware(ILogger logger) { - _next = next; _logger = logger; _clearCacheHeadersDelegate = ClearCacheHeaders; } - public async Task Invoke(HttpContext httpContext) + public async Task InvokeAsync(HttpContext context, RequestDelegate next) { try { - await _next(httpContext); + await next(context); } catch (Exception ex) { // We can't do anything if the response has already started, just abort. - if (httpContext.Response.HasStarted) + if (context.Response.HasStarted) { _logger.LogWarning("An exception occurred, but response has already started!"); throw; } - if (httpContext.Items["_AbpActionInfo"] is AbpActionInfoInHttpContext actionInfo) + if (context.Items["_AbpActionInfo"] is AbpActionInfoInHttpContext actionInfo) { if (actionInfo.IsObjectResult) //TODO: Align with AbpExceptionFilter.ShouldHandleException! { - await HandleAndWrapException(httpContext, ex); + await HandleAndWrapException(context, ex); return; } } diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs index c96a760aad..5c7b4cdff7 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs @@ -5,30 +5,29 @@ using Volo.Abp.Tracing; namespace Volo.Abp.AspNetCore.Tracing { - public class AbpCorrelationIdMiddleware + public class AbpCorrelationIdMiddleware : IMiddleware { - private readonly RequestDelegate _next; + private readonly CorrelationIdOptions _options; + private readonly ICorrelationIdProvider _correlationIdProvider; - public AbpCorrelationIdMiddleware(RequestDelegate next) + public AbpCorrelationIdMiddleware(IOptions options, + ICorrelationIdProvider correlationIdProvider) { - _next = next; + _options = options.Value; + _correlationIdProvider = correlationIdProvider; } - public async Task Invoke( - HttpContext httpContext, - IOptions options, - ICorrelationIdProvider correlationIdProvider) + public async Task InvokeAsync(HttpContext context, RequestDelegate next) { - var correlationId = correlationIdProvider.Get(); - var optionsValue = options.Value; + var correlationId = _correlationIdProvider.Get(); try { - await _next(httpContext); + await next(context); } finally { - CheckAndSetCorrelationIdOnResponse(httpContext, optionsValue, correlationId); + CheckAndSetCorrelationIdOnResponse(context, _options, correlationId); } } diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs index d2cb4685b9..1311050ba0 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs @@ -4,25 +4,23 @@ using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Uow { - public class AbpUnitOfWorkMiddleware + public class AbpUnitOfWorkMiddleware : IMiddleware { public const string UnitOfWorkReservationName = "_AbpActionUnitOfWork"; - private readonly RequestDelegate _next; private readonly IUnitOfWorkManager _unitOfWorkManager; - public AbpUnitOfWorkMiddleware(RequestDelegate next, IUnitOfWorkManager unitOfWorkManager) + public AbpUnitOfWorkMiddleware(IUnitOfWorkManager unitOfWorkManager) { - _next = next; _unitOfWorkManager = unitOfWorkManager; } - public async Task Invoke(HttpContext httpContext) + public async Task InvokeAsync(HttpContext context, RequestDelegate next) { using (var uow = _unitOfWorkManager.Reserve(UnitOfWorkReservationName)) { - await _next(httpContext); - await uow.CompleteAsync(httpContext.RequestAborted); + await next(context); + await uow.CompleteAsync(context.RequestAborted); } } } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs index eb0fc1ef48..400aa8f570 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs @@ -70,6 +70,8 @@ namespace Volo.Abp.AspNetCore.Mvc typeof(AbpValidationResource) ).AddVirtualJson("/Volo/Abp/AspNetCore/Mvc/Localization/Resource"); }); + + context.Services.AddTransient(); } public override void OnApplicationInitialization(ApplicationInitializationContext context) diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/FakeAuthenticationMiddleware.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/FakeAuthenticationMiddleware.cs index 6f9aef0520..035aa8b925 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/FakeAuthenticationMiddleware.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/FakeAuthenticationMiddleware.cs @@ -6,28 +6,26 @@ using Microsoft.AspNetCore.Http; namespace Volo.Abp.AspNetCore.Mvc.Authorization { - public class FakeAuthenticationMiddleware + public class FakeAuthenticationMiddleware : IMiddleware { - private readonly RequestDelegate _next; private readonly FakeUserClaims _fakeUserClaims; - public FakeAuthenticationMiddleware(RequestDelegate next, FakeUserClaims fakeUserClaims) + public FakeAuthenticationMiddleware(FakeUserClaims fakeUserClaims) { - _next = next; _fakeUserClaims = fakeUserClaims; } - public async Task Invoke(HttpContext httpContext) + public async Task InvokeAsync(HttpContext context, RequestDelegate next) { if (_fakeUserClaims.Claims.Any()) { - httpContext.User = new ClaimsPrincipal(new List + context.User = new ClaimsPrincipal(new List { new ClaimsIdentity(_fakeUserClaims.Claims, "FakeSchema") }); } - await _next(httpContext); + await next(context); } } } \ No newline at end of file