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.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public class MultiTenancyMiddleware
public class MultiTenancyMiddleware : IMiddleware, ITransientDependency
{
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 +45,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
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.Extensions.Options;
using Volo.Abp.Auditing;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Users;
namespace Volo.Abp.AspNetCore.Auditing
{
public class AbpAuditingMiddleware
public class AbpAuditingMiddleware : IMiddleware, ITransientDependency
{
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<AbpAuditingOptions> 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 +38,7 @@ namespace Volo.Abp.AspNetCore.Auditing
{
try
{
await _next(httpContext);
await next(context);
}
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.Net.Http.Headers;
using Volo.Abp.AspNetCore.Uow;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http;
using Volo.Abp.Json;
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 Func<object, Task> _clearCacheHeadersDelegate;
public AbpExceptionHandlingMiddleware(RequestDelegate next, ILogger<AbpUnitOfWorkMiddleware> logger)
public AbpExceptionHandlingMiddleware(ILogger<AbpUnitOfWorkMiddleware> 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;
}
}

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

@ -1,34 +1,34 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.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(
HttpContext httpContext,
IOptions<CorrelationIdOptions> 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);
}
}

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

@ -1,28 +1,27 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Uow;
namespace Volo.Abp.AspNetCore.Uow
{
public class AbpUnitOfWorkMiddleware
public class AbpUnitOfWorkMiddleware : IMiddleware, ITransientDependency
{
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);
}
}
}

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.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Mvc.Authorization
{
public class FakeAuthenticationMiddleware
public class FakeAuthenticationMiddleware : IMiddleware, ITransientDependency
{
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<ClaimsIdentity>
context.User = new ClaimsPrincipal(new List<ClaimsIdentity>
{
new ClaimsIdentity(_fakeUserClaims.Claims, "FakeSchema")
});
}
await _next(httpContext);
await next(context);
}
}
}
Loading…
Cancel
Save