Beispiel #1
0
        /// <summary>
        /// Injects Custom ASP.NET Core Application exception handling to <see cref="IExceptionHandlerFeature"/>
        /// </summary>
        /// <param name="applicationBuilder"><see cref="IExceptionHandlerFeature"/></param>
        /// <returns><see cref="IApplicationBuilder"/> instance</returns>
        public static IApplicationBuilder UseExceptionHandlingFilter(this IApplicationBuilder applicationBuilder)
        {
            applicationBuilder.UseExceptionHandler(
                options =>
            {
                options.Run(
                    async context =>
                {
                    var ex = context.Features.Get <IExceptionHandlerFeature>();
                    if (ex != null)
                    {
                        IActionResultHandler actionResultHandler = context.RequestServices.GetService(typeof(IActionResultHandler)) as IActionResultHandler;
                        await actionResultHandler?.HandleActionResult(new ActionResultContext(context, ex.Error));
                    }
                });
            }
                );

            return(applicationBuilder);
        }
Beispiel #2
0
        /// <summary>
        /// Executes the <see cref="ExceptionHandlingMiddleware"/> to catch and handle exception using <see cref="IExceptionManager"/>
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context)
        {
            string url          = context.Request.GetDisplayUrl();
            var    requestScope = string.Format("{0} : requestScope - {1}", url, Guid.NewGuid());

            try
            {
                await _next(context);

                //await _actionResultHandler?.HandleActionResult(new ActionResultContext(_exceptionManager, context, requestScope));
            }
            catch (Exception exception)
            {
                ExceptionCodeDetails details = _exceptionCodesDecider.DecideExceptionCode(exception);
                IActionResult        result  = details == null
                    ? _exceptionManager.ManageException(exception)
                    : _exceptionManager.ManageException(details.ExceptionCode, exception, details.Params);

                await _actionResultHandler?.HandleActionResult(new ActionResultContext(context, exception, result));
            }
        }