Browse Source

Merge pull request #1274 from abpframework/maliming/issue1268

Resolve #1268 Implement IMiddleware for all asp.net core middlewares.
pull/1281/head
Halil İbrahim Kalkan 7 years ago
committed by GitHub
parent
commit
5e9d4490e9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs
  2. 14
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs
  3. 17
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs
  4. 24
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs
  5. 13
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs
  6. 13
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/FakeAuthenticationMiddleware.cs

11
framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs

@ -1,34 +1,31 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
namespace Volo.Abp.AspNetCore.MultiTenancy namespace Volo.Abp.AspNetCore.MultiTenancy
{ {
public class MultiTenancyMiddleware public class MultiTenancyMiddleware : IMiddleware, ITransientDependency
{ {
private readonly RequestDelegate _next;
private readonly ITenantResolver _tenantResolver; private readonly ITenantResolver _tenantResolver;
private readonly ITenantStore _tenantStore; private readonly ITenantStore _tenantStore;
private readonly ICurrentTenant _currentTenant; private readonly ICurrentTenant _currentTenant;
private readonly ITenantResolveResultAccessor _tenantResolveResultAccessor; private readonly ITenantResolveResultAccessor _tenantResolveResultAccessor;
public MultiTenancyMiddleware( public MultiTenancyMiddleware(
RequestDelegate next,
ITenantResolver tenantResolver, ITenantResolver tenantResolver,
ITenantStore tenantStore, ITenantStore tenantStore,
ICurrentTenant currentTenant, ICurrentTenant currentTenant,
ITenantResolveResultAccessor tenantResolveResultAccessor) ITenantResolveResultAccessor tenantResolveResultAccessor)
{ {
_next = next;
_tenantResolver = tenantResolver; _tenantResolver = tenantResolver;
_tenantStore = tenantStore; _tenantStore = tenantStore;
_currentTenant = currentTenant; _currentTenant = currentTenant;
_tenantResolveResultAccessor = tenantResolveResultAccessor; _tenantResolveResultAccessor = tenantResolveResultAccessor;
} }
public async Task Invoke(HttpContext httpContext) public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{ {
var resolveResult = _tenantResolver.ResolveTenantIdOrName(); var resolveResult = _tenantResolver.ResolveTenantIdOrName();
_tenantResolveResultAccessor.Result = resolveResult; _tenantResolveResultAccessor.Result = resolveResult;
@ -48,7 +45,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
using (_currentTenant.Change(tenant?.Id, tenant?.Name)) using (_currentTenant.Change(tenant?.Id, tenant?.Name))
{ {
await _next(httpContext); await next(context);
} }
} }

14
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs

@ -3,36 +3,34 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.Auditing; using Volo.Abp.Auditing;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Users; using Volo.Abp.Users;
namespace Volo.Abp.AspNetCore.Auditing namespace Volo.Abp.AspNetCore.Auditing
{ {
public class AbpAuditingMiddleware public class AbpAuditingMiddleware : IMiddleware, ITransientDependency
{ {
private readonly RequestDelegate _next;
private readonly IAuditingManager _auditingManager; private readonly IAuditingManager _auditingManager;
protected AbpAuditingOptions Options { get; } protected AbpAuditingOptions Options { get; }
protected ICurrentUser CurrentUser { get; } protected ICurrentUser CurrentUser { get; }
public AbpAuditingMiddleware( public AbpAuditingMiddleware(
RequestDelegate next,
IAuditingManager auditingManager, IAuditingManager auditingManager,
ICurrentUser currentUser, ICurrentUser currentUser,
IOptions<AbpAuditingOptions> options) IOptions<AbpAuditingOptions> options)
{ {
_next = next;
_auditingManager = auditingManager; _auditingManager = auditingManager;
CurrentUser = currentUser; CurrentUser = currentUser;
Options = options.Value; 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; return;
} }
@ -40,7 +38,7 @@ namespace Volo.Abp.AspNetCore.Auditing
{ {
try try
{ {
await _next(httpContext); await next(context);
} }
finally finally
{ {

17
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs

@ -5,46 +5,45 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
using Volo.Abp.AspNetCore.Uow; using Volo.Abp.AspNetCore.Uow;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http; using Volo.Abp.Http;
using Volo.Abp.Json; using Volo.Abp.Json;
namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling
{ {
public class AbpExceptionHandlingMiddleware public class AbpExceptionHandlingMiddleware : IMiddleware, ITransientDependency
{ {
private readonly RequestDelegate _next;
private readonly ILogger<AbpUnitOfWorkMiddleware> _logger; private readonly ILogger<AbpUnitOfWorkMiddleware> _logger;
private readonly Func<object, Task> _clearCacheHeadersDelegate; private readonly Func<object, Task> _clearCacheHeadersDelegate;
public AbpExceptionHandlingMiddleware(RequestDelegate next, ILogger<AbpUnitOfWorkMiddleware> logger) public AbpExceptionHandlingMiddleware(ILogger<AbpUnitOfWorkMiddleware> logger)
{ {
_next = next;
_logger = logger; _logger = logger;
_clearCacheHeadersDelegate = ClearCacheHeaders; _clearCacheHeadersDelegate = ClearCacheHeaders;
} }
public async Task Invoke(HttpContext httpContext) public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{ {
try try
{ {
await _next(httpContext); await next(context);
} }
catch (Exception ex) catch (Exception ex)
{ {
// We can't do anything if the response has already started, just abort. // 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!"); _logger.LogWarning("An exception occurred, but response has already started!");
throw; throw;
} }
if (httpContext.Items["_AbpActionInfo"] is AbpActionInfoInHttpContext actionInfo) if (context.Items["_AbpActionInfo"] is AbpActionInfoInHttpContext actionInfo)
{ {
if (actionInfo.IsObjectResult) //TODO: Align with AbpExceptionFilter.ShouldHandleException! if (actionInfo.IsObjectResult) //TODO: Align with AbpExceptionFilter.ShouldHandleException!
{ {
await HandleAndWrapException(httpContext, ex); await HandleAndWrapException(context, ex);
return; return;
} }
} }

24
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs

@ -1,34 +1,34 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Tracing; using Volo.Abp.Tracing;
namespace Volo.Abp.AspNetCore.Tracing namespace Volo.Abp.AspNetCore.Tracing
{ {
public class AbpCorrelationIdMiddleware public class AbpCorrelationIdMiddleware : IMiddleware, ITransientDependency
{ {
private readonly RequestDelegate _next; private readonly CorrelationIdOptions _options;
private readonly ICorrelationIdProvider _correlationIdProvider;
public AbpCorrelationIdMiddleware(RequestDelegate next) public AbpCorrelationIdMiddleware(IOptions<CorrelationIdOptions> options,
ICorrelationIdProvider correlationIdProvider)
{ {
_next = next; _options = options.Value;
_correlationIdProvider = correlationIdProvider;
} }
public async Task Invoke( public async Task InvokeAsync(HttpContext context, RequestDelegate next)
HttpContext httpContext,
IOptions<CorrelationIdOptions> options,
ICorrelationIdProvider correlationIdProvider)
{ {
var correlationId = correlationIdProvider.Get(); var correlationId = _correlationIdProvider.Get();
var optionsValue = options.Value;
try try
{ {
await _next(httpContext); await next(context);
} }
finally finally
{ {
CheckAndSetCorrelationIdOnResponse(httpContext, optionsValue, correlationId); CheckAndSetCorrelationIdOnResponse(context, _options, correlationId);
} }
} }

13
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs

@ -1,28 +1,27 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Uow; using Volo.Abp.Uow;
namespace Volo.Abp.AspNetCore.Uow namespace Volo.Abp.AspNetCore.Uow
{ {
public class AbpUnitOfWorkMiddleware public class AbpUnitOfWorkMiddleware : IMiddleware, ITransientDependency
{ {
public const string UnitOfWorkReservationName = "_AbpActionUnitOfWork"; public const string UnitOfWorkReservationName = "_AbpActionUnitOfWork";
private readonly RequestDelegate _next;
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
public AbpUnitOfWorkMiddleware(RequestDelegate next, IUnitOfWorkManager unitOfWorkManager) public AbpUnitOfWorkMiddleware(IUnitOfWorkManager unitOfWorkManager)
{ {
_next = next;
_unitOfWorkManager = unitOfWorkManager; _unitOfWorkManager = unitOfWorkManager;
} }
public async Task Invoke(HttpContext httpContext) public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{ {
using (var uow = _unitOfWorkManager.Reserve(UnitOfWorkReservationName)) using (var uow = _unitOfWorkManager.Reserve(UnitOfWorkReservationName))
{ {
await _next(httpContext); await next(context);
await uow.CompleteAsync(httpContext.RequestAborted); await uow.CompleteAsync(context.RequestAborted);
} }
} }
} }

13
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/FakeAuthenticationMiddleware.cs

@ -3,31 +3,30 @@ using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Mvc.Authorization namespace Volo.Abp.AspNetCore.Mvc.Authorization
{ {
public class FakeAuthenticationMiddleware public class FakeAuthenticationMiddleware : IMiddleware, ITransientDependency
{ {
private readonly RequestDelegate _next;
private readonly FakeUserClaims _fakeUserClaims; private readonly FakeUserClaims _fakeUserClaims;
public FakeAuthenticationMiddleware(RequestDelegate next, FakeUserClaims fakeUserClaims) public FakeAuthenticationMiddleware(FakeUserClaims fakeUserClaims)
{ {
_next = next;
_fakeUserClaims = fakeUserClaims; _fakeUserClaims = fakeUserClaims;
} }
public async Task Invoke(HttpContext httpContext) public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{ {
if (_fakeUserClaims.Claims.Any()) if (_fakeUserClaims.Claims.Any())
{ {
httpContext.User = new ClaimsPrincipal(new List<ClaimsIdentity> context.User = new ClaimsPrincipal(new List<ClaimsIdentity>
{ {
new ClaimsIdentity(_fakeUserClaims.Claims, "FakeSchema") new ClaimsIdentity(_fakeUserClaims.Claims, "FakeSchema")
}); });
} }
await _next(httpContext); await next(context);
} }
} }
} }
Loading…
Cancel
Save