Admin
2025-12-02 9e42f0dafa019f5ecf6b0ff425ecb966b002171e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//using WIDESEA.Core.Enums;
//using WIDESEA.Entity.DomainModels.ResponseEntity;
//using WIDESEA.Entity.SystemsModels;
//using Microsoft.AspNetCore.Http;
//using Newtonsoft.Json;
//using System;
//using System.IO;
//using System.Net;
//using System.Text;
//using System.Threading.Tasks;
//using System.Xml.Serialization;
 
//namespace WIDESEA.Core.Extensions.Middleware
//{
//    public class ExceptionHandlerMiddleWare
//    {
//        private readonly RequestDelegate next;
 
//        public ExceptionHandlerMiddleWare(RequestDelegate next)
//        {
//            this.next = next;
//        }
 
//        public async Task Invoke(HttpContext context)
//        {
//            try
//            {
//                await next(context);
//            }
//            catch (Exception ex)
//            {
//                await HandleExceptionAsync(context, ex);
//            }
//        }
 
//        private static async Task HandleExceptionAsync(HttpContext context, Exception exception)
//        {
//            if (exception == null) return;
//            await WriteExceptionAsync(context, exception).ConfigureAwait(false);
//        }
 
//        private static async Task WriteExceptionAsync(HttpContext context, Exception exception)
//        {
 
//            Logger.WriteLog(LogEnum.Exception, null, exception.Message + exception.StackTrace, null);
 
//            var response = context.Response;
 
//            if (exception is UnauthorizedAccessException)
//                response.StatusCode = (int)HttpStatusCode.Unauthorized;
//            else if (exception is Exception)
//                //  response.StatusCode = (int)HttpStatusCode.BadRequest;
//                response.StatusCode = (int)HttpStatusCode.InternalServerError;
//            response.ContentType = context.Request.Headers["Accept"];
 
//            //if (response.ContentType.ToLower() == "application/xml")
//            response.ContentType = "application/json";
//            ResponseData responseData = new ResponseData();
//            responseData.SetResponseValue(ResponseType.ServerError);
//            string reslutMsg = JsonConvert.SerializeObject(responseData);
 
//            await response.WriteAsync(reslutMsg, Encoding.UTF8).ConfigureAwait(false);
//        }
 
//        /// <summary>
//        /// 对象转为Xml
//        /// </summary>
//        /// <param name="o"></param>
//        /// <returns></returns>
//        private static string Object2XmlString(object o)
//        {
//            StringWriter sw = new StringWriter();
//            try
//            {
//                XmlSerializer serializer = new XmlSerializer(o.GetType());
//                serializer.Serialize(sw, o);
//            }
//            catch
//            {
//                //Handle Exception Code
//            }
//            finally
//            {
//                sw.Dispose();
//            }
//            return sw.ToString();
//        }
 
//    }
//}