/// <summary> /// ExceptionHandlingMiddleware /// </summary> public ExceptionHandlingMiddleware( RequestDelegate next, ILoggerFactory loggerFactory, IOptions <CustomExceptionHandlerOptions> options, IOptions <MvcJsonOptions> mvcJsonOptions) { _next = next; _jsonSerializer = mvcJsonOptions.Value.SerializerSettings; _options = options.Value; _logger = loggerFactory.CreateLogger <ExceptionHandlingMiddleware>(); if (_options.ExceptionHandler != null) { return; } if (_options.ExceptionHandlingPath == null) { throw new InvalidOperationException("请求的异常处理地址为空"); } _options.ExceptionHandler = _next; }
/// <summary> /// 从管道捕获同步和异步实例参见 <see cref="T:System.Exception"/> 并生成HTML错误响应。 /// </summary> /// <returns>操作完成后app</returns> public static IApplicationBuilder UseCustomExceptionHandler(this IApplicationBuilder app, CustomExceptionHandlerOptions options) { if (app == null) { throw new ArgumentNullException(nameof(app)); } return(app.UseMiddleware <ExceptionHandlingMiddleware>((object)Options.Create(options))); }