fix vali middleware

This commit is contained in:
HTR
2018-07-02 18:15:35 +08:00
parent 2e19235619
commit 1b5155aeaa
14 changed files with 358 additions and 226 deletions
+11
View File
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.0" />
</ItemGroup>
</Project>
@@ -0,0 +1,33 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace Lib.Core.MiddlewareExtension
{
public class ExceptionHandlingMiddleware
{
private readonly RequestDelegate _next;
private readonly IExceptionHandler _exceptionHandler;
public ExceptionHandlingMiddleware(RequestDelegate next, IExceptionHandler exceptionHandler)
{
_next = next;
_exceptionHandler = exceptionHandler;
}
public async Task Invoke(HttpContext context, ILogger<ExceptionHandlingMiddleware> logger)
{
try
{
await _next(context);
}
catch (Exception ex)
{
logger.LogDebug(ex, "ExceptionHandlingMiddleware");
await _exceptionHandler.ExceptionHandle(context, ex);
}
}
}
}
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Builder;
namespace Lib.Core.MiddlewareExtension.Extension
{
public static class MiddlewareExtension
{
public static IApplicationBuilder UseMiddlewareExtension(this IApplicationBuilder app, IExceptionHandler exceptionHandler)
{
app.UseMiddleware<ExceptionHandlingMiddleware>(exceptionHandler);
return app;
}
}
}
@@ -0,0 +1,11 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace Lib.Core.MiddlewareExtension
{
public interface IExceptionHandler
{
Task ExceptionHandle(HttpContext context, Exception exception);
}
}