1
This commit is contained in:
49
iMES.Core/Middleware/ExceptionHandlerMiddleWare.cs
Normal file
49
iMES.Core/Middleware/ExceptionHandlerMiddleWare.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using iMES.Core.Const;
|
||||
using iMES.Core.EFDbContext;
|
||||
using iMES.Core.Enums;
|
||||
using iMES.Core.Extensions;
|
||||
using iMES.Core.ManageUser;
|
||||
using iMES.Core.Services;
|
||||
|
||||
namespace iMES.Core.Middleware
|
||||
{
|
||||
public class ExceptionHandlerMiddleWare
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
public ExceptionHandlerMiddleWare(RequestDelegate next)
|
||||
{
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
(context.RequestServices.GetService(typeof(ActionObserver)) as ActionObserver).RequestDate = DateTime.Now;
|
||||
await next(context);
|
||||
Logger.Info(LoggerType.System);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
// (context.RequestServices.GetService(typeof(ActionObserver)) as ActionObserver).IsWrite = false;
|
||||
string message = exception.Message
|
||||
+ exception.InnerException
|
||||
?.InnerException
|
||||
?.Message
|
||||
+ exception.InnerException
|
||||
+ exception.StackTrace;
|
||||
Console.WriteLine($"服务器处理出现异常:{message}");
|
||||
Logger.Error(LoggerType.Exception, message);
|
||||
context.Response.StatusCode = 500;
|
||||
context.Response.ContentType = ApplicationContentType.JSON;
|
||||
await context.Response.WriteAsync(new { message = "~服务器没有正确处理请求,请稍等再试!", status = false }.Serialize()
|
||||
, Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
56
iMES.Core/Middleware/HttpRequestMiddleware.cs
Normal file
56
iMES.Core/Middleware/HttpRequestMiddleware.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace iMES.Core.Middleware
|
||||
{
|
||||
public class HttpRequestMiddleware
|
||||
{
|
||||
public static Func<RequestDelegate, RequestDelegate> Context
|
||||
{
|
||||
get
|
||||
{
|
||||
return next => async context =>
|
||||
{
|
||||
//动态标识刷新token(2021.05.01)
|
||||
context.Response.Headers.Add("Access-Control-Expose-Headers", "vol_exp");
|
||||
var stream = context.Request.Body;
|
||||
if (stream == Stream.Null || stream.CanSeek)
|
||||
{
|
||||
await next(context);
|
||||
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
using (var buffer = new MemoryStream())
|
||||
{
|
||||
// Copy the request stream to the memory stream.
|
||||
await stream.CopyToAsync(buffer);
|
||||
|
||||
// Rewind the memory stream.
|
||||
buffer.Position = 0L;
|
||||
|
||||
// Replace the request stream by the memory stream.
|
||||
context.Request.Body = buffer;
|
||||
|
||||
// Invoke the rest of the pipeline.
|
||||
await next(context);
|
||||
}
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
// Restore the original stream.
|
||||
context.Request.Body = stream;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user