Esempio n. 1
0
        public static async Task HandleGlobalExceptionAsync(HttpContext context)
        {
            var exception =
                context.Features.Get <IExceptionHandlerPathFeature>()?.Error;
            HttpStatusCode statusCode;

            KoboWashResult result;

            // handle custom errors that will be thrown at by the application
            if (exception is KoboWashException)
            {
                result     = new KoboWashResult(((KoboWashException)exception).Code, exception.Message);
                statusCode = HttpStatusCode.BadRequest;
                //return applicationResponse;
            }
            else
            {
                // handle unregistered errors thrown by the application
                statusCode = HttpStatusCode.InternalServerError;
                result     = new KoboWashResult(ResponseCodesList.InternalServerError, exception?.Message);
                //return applicationResponse;
            }

            context.Response.StatusCode  = (int)statusCode;
            context.Response.ContentType = "application/json";
            context.Response.Headers.Remove("X-ERROR");
            context.Response.Headers.Add("X-ERROR", "true");
            await context.Response.WriteAsync(JsonConvert.SerializeObject(result));
        }
Esempio n. 2
0
        public async Task Invoke(HttpContext context)
        {
            string remoteIpAddress = context.Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();

            if (context.Request.Headers.ContainsKey("X-Forwarded-For"))
            {
                remoteIpAddress = context.Request.Headers["X-Forwarded-For"];
            }

            string.Join(" ", context.Request.Headers);
            var conn = string.Join("", context.Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString());

            Log.Information("Client details headers {IP}, {Headers}", remoteIpAddress, conn);


            var model = new RequestProfilerModel
            {
                RequestTime = new DateTimeOffset(),
                Context     = context,
                Request     = await FormatRequest(context)
            };

            Stream originalBody = context.Response.Body;

            using (MemoryStream newResponseBody = _recyclableMemoryStreamManager.GetStream())
            {
                context.Response.Body = newResponseBody;
                await _next(context);

                newResponseBody.Seek(0, SeekOrigin.Begin);
                String         response = LocalEncoding.GetString(newResponseBody.ToArray());
                string         code     = ResponseCodesList.Success;
                string         message  = "Successful";
                KoboWashResult newBody  = JsonConvert.DeserializeObject <KoboWashResult>(response);
                Object         data     = null;
                if (newBody.ResponseCode != null)
                {
                    code    = newBody.ResponseCode;
                    message = newBody.ResponseMessage;
                }
                else
                {
                    data = JsonConvert.DeserializeObject(response);
                }

                KoboWashResult res = new KoboWashResult(code, message)
                {
                    Data = data
                };
                String result = JsonConvert.SerializeObject(res);
                _logger.Log(LogLevel.Information, result);
                byte[]       byteArray = Encoding.ASCII.GetBytes(result);
                MemoryStream stream    = new MemoryStream(byteArray);
                await stream.CopyToAsync(originalBody);

                stream.Seek(0, SeekOrigin.Begin);
                model.Response     = FormatResponse(context, stream);
                model.ResponseTime = new DateTimeOffset();
                _requestResponseHandler(model);
            }
        }