Browse Source

Resolve #1268 Implement IMiddleware for all asp.net core middlewares.

pull/1274/head
maliming 7 years ago
parent
commit
674291a928
  1. 2
      framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyModule.cs
  2. 10
      framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs
  3. 8
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs
  4. 13
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs
  5. 16
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs
  6. 23
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs
  7. 12
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs
  8. 2
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs
  9. 12
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/FakeAuthenticationMiddleware.cs

2
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<MultiTenancyMiddleware>();
}
}
}

10
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);
}
}

8
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<IApplicationBuilder>();
context.Services.AddTransient<AbpAuditingMiddleware>();
context.Services.AddTransient<AbpUnitOfWorkMiddleware>();
context.Services.AddTransient<AbpCorrelationIdMiddleware>();
context.Services.AddTransient<AbpExceptionHandlingMiddleware>();
}
private static void AddAspNetServices(IServiceCollection services)

13
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<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 +37,7 @@ namespace Volo.Abp.AspNetCore.Auditing
{
try
{
await _next(httpContext);
await next(context);
}
finally
{

16
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<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;
}
}

23
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<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);
}
}

12
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);
}
}
}

2
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<FakeAuthenticationMiddleware>();
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)

12
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<ClaimsIdentity>
context.User = new ClaimsPrincipal(new List<ClaimsIdentity>
{
new ClaimsIdentity(_fakeUserClaims.Claims, "FakeSchema")
});
}
await _next(httpContext);
await next(context);
}
}
}
Loading…
Cancel
Save