自行实现高性能MVC WebAPI - yswenli - 博客园

自行实现高性能MVC WebAPI

  wcf虽然功能多、扩展性强但是也面临配置忒多,而且restful的功能相当怪异,并且目前没法移植。asp.net core虽然支持webapi,但是功能也相对繁多、配置复杂。就没有一个能让码农们安安心心的写webapi,无需考虑性能、配置、甚至根据问题场景自行设计、改造等问题的方案么?当然不是,特别是在dnc2.0已经相当强大的此时,完全可以自行设计一套简洁、高效的webapi框架!说到自行写一套框架,很多码农们就可能会想到开发工作量难以想像,事实真的如此么?面对日益强大的dnc,本人就不是很信这个邪,所以就亲自编了一个试试。

      设计的编码思路就是仿asp.net mvc,原因就是asp.net mvc成功发展了这么多年,有着大量的码农习惯了这套优良的编码方式,那么最终实现效果如下:

 1 /****************************************************************************
 2 *Copyright (c) 2018 Microsoft All Rights Reserved.
 3 *CLR版本: 4.0.30319.42000
 4 *机器名称:WENLI-PC
 5 *公司名称:Microsoft
 6 *命名空间:SAEA.WebAPITest.Controllers
 7 *文件名: HomeController
 8 *版本号: V1.0.0.0
 9 *唯一标识:e00bb57f-e3ee-4efe-a7cf-f23db767c1d0
10 *当前的用户域:WENLI-PC
11 *创建人: yswenli
12 *电子邮箱:wenguoli_520@qq.com
13 *创建时间:2018/4/10 16:43:26
14 *描述:
15 *
16 *=====================================================================
17 *修改标记
18 *修改时间:2018/4/10 16:43:26
19 *修改人: yswenli
20 *版本号: V1.0.0.0
21 *描述:
22 *
23 *****************************************************************************/
24 using SAEA.WebAPI.Mvc;
25 using SAEA.WebAPITest.Attrubutes;
26 
27 namespace SAEA.WebAPITest.Controllers
28 {
29     [LogAtrribute]
30     public class HomeController : Controller
31     {
32         public ActionResult Index()
33         {
34             return Content("Hello,I'm SAEA.WebAPI!");
35         }
36 
37         public ActionResult Get(int id)
38         {
39             return Json(new { Name = "yswenli", Sex = "" });
40         }
41 
42         public ActionResult Show()
43         {
44             var response = HttpContext.Response;
45 
46             response.Content_Type = "text/html; charset=utf-8";
47 
48             response.Write("<h3>测试一下那个response对象使用情况!</h3>参考消息网4月12日报道外媒称,法国一架“幻影-2000”");            
49 
50             response.End();
51 
52             return Empty();
53         }
54 
55         public ActionResult Download()
56         {
57             return File(HttpContext.Server.MapPath("/Content/Image/c984b2fb80aeca7b15eda8c004f2e0d4.jpg"));
58         }
59     }
60 }

  从上面的代码风格出发,那么需要考虑的问题有tcp、http、request、response、server、controller、actionresult、routetable等,下面就一一来解决这个问题。

  一、Tcp:这个当然采用IOCP来提高吞吐量,本人之前在做Redis Client等的时候就使用这个IOCP Socket的框架,此时正好也可以用上

 1 /****************************************************************************
 2 *Copyright (c) 2018 Microsoft All Rights Reserved.
 3 *CLR版本: 4.0.30319.42000
 4 *机器名称:WENLI-PC
 5 *公司名称:Microsoft
 6 *命名空间:SAEA.WebAPI.Http.Net
 7 *文件名: ServerSocket
 8 *版本号: V1.0.0.0
 9 *唯一标识:ab912b9a-c7ed-44d9-8e48-eef0b6ff86a2
10 *当前的用户域:WENLI-PC
11 *创建人: yswenli
12 *电子邮箱:wenguoli_520@qq.com
13 *创建时间:2018/4/8 17:11:15
14 *描述:
15 *
16 *=====================================================================
17 *修改标记
18 *修改时间:2018/4/8 17:11:15
19 *修改人: yswenli
20 *版本号: V1.0.0.0
21 *描述:
22 *
23 *****************************************************************************/
24 using SAEA.Sockets.Core;
25 using SAEA.Sockets.Interface;
26 using System;
27 using System.Collections.Generic;
28 using System.Net;
29 using System.Text;
30 
31 namespace SAEA.WebAPI.Http.Net
32 {
33     class ServerSocket : BaseServerSocket
34     {
35         public event Action<IUserToken, string> OnRequested;
36 
37         public ServerSocket(int bufferSize = 1024 * 100, int count = 10000) : base(new HContext(), bufferSize, true, count)
38         {
39 
40         }
41 
42         protected override void OnReceiveBytes(IUserToken userToken, byte[] data)
43         {
44             HCoder coder = (HCoder)userToken.Coder;
45 
46             coder.GetRequest(data, (result) =>
47             {
48                 OnRequested?.Invoke(userToken, result);
49             });
50         }
51 
52         public void Reply(IUserToken userToken, byte[] data)
53         {
54             base.Send(userToken, data);
55             base.Disconnected(userToken);
56         }
57     }
58 }

  二、Http:这个只需要了解一下相关的传输就行,本人的做法就是用Chrome的开发人员工具来查看相关network详情,这样的话就可以实现tcp传输的解码问题了

 1         public void GetRequest(byte[] data, Action<string> onUnpackage)
 2         {
 3             lock (_locker)
 4             {
 5                 var str = Encoding.UTF8.GetString(data);
 6 
 7                 var index = str.IndexOf(ENDSTR);
 8 
 9                 if (index > -1)
10                 {
11                     var s = str.Substring(0, index);
12 
13                     _result.Append(s);
14 
15                     onUnpackage.Invoke(_result.ToString());
16 
17                     _result.Clear();
18 
19                     if (str.Length > index + 4)
20                     {
21                         _result.Append(str.Substring(index + 4));
22                     }
23                 }
24                 else
25                 {
26                     _result.Append(str);
27                 }
28             }
29         }

  解决了tcp的问题,还得简单分析http的解码,http的内容其实就是字符回车分隔。

 1         public HttpRequest(Stream stream)
 2         {
 3             this._dataStream = stream;
 4             var data = GetRequestData(_dataStream);
 5             var rows = Regex.Split(data, Environment.NewLine);
 6 
 7             //Request URL & Method & Version
 8             var first = Regex.Split(rows[0], @"(\s+)")
 9                 .Where(e => e.Trim() != string.Empty)
10                 .ToArray();
11             if (first.Length > 0) this.Method = first[0];
12             if (first.Length > 1)
13             {
14                 this.Query = first[1];
15 
16                 if (this.Query.Contains("?"))
17                 {
18                     var qarr = this.Query.Split("?");
19                     this.URL = qarr[0];
20                     this.Params = GetRequestParameters(qarr[1]);
21                 }
22                 else
23                 {
24                     this.URL = this.Query;
25                 }
26 
27                 var uarr = this.URL.Split("/");
28 
29                 if (long.TryParse(uarr[uarr.Length - 1], out long id))
30                 {
31                     this.URL = this.URL.Substring(0, this.URL.LastIndexOf("/"));
32                     this.Params.Set("id", id.ToString());
33                 }
34             }
35             if (first.Length > 2) this.Protocols = first[2];
36 
37             //Request Headers
38             this.Headers = GetRequestHeaders(rows);
39 
40             //Request "GET"
41             if (this.Method == "GET")
42             {
43                 this.Body = GetRequestBody(rows);
44             }
45 
46             //Request "POST"
47             if (this.Method == "POST")
48             {
49                 this.Body = GetRequestBody(rows);
50                 var contentType = GetHeader(RequestHeaderType.ContentType);
51                 var isUrlencoded = contentType == @"application/x-www-form-urlencoded";
52                 if (isUrlencoded) this.Params = GetRequestParameters(this.Body);
53             }
54         }

  看到上面,有人肯定会说你这个传文件咋办?一个呢本人这个是针对webapi;另外一个,如真有这个场景,可以用Chrome的开发人员工具来查看相关network详情,也可以使用httpanalyzerstd、httpwatch等众多工具分析下,然后自行扩展~

  三、Reponse这个是webapi服务端相当重要的一个组件,本人也是尽可能方便并且按asp.net的命名来实现

  1 /****************************************************************************
  2 *Copyright (c) 2018 Microsoft All Rights Reserved.
  3 *CLR版本: 4.0.30319.42000
  4 *机器名称:WENLI-PC
  5 *公司名称:Microsoft
  6 *命名空间:SAEA.WebAPI.Http
  7 *文件名: HttpResponse
  8 *版本号: V1.0.0.0
  9 *唯一标识:2e43075f-a43d-4b60-bee1-1f9107e2d133
 10 *当前的用户域:WENLI-PC
 11 *创建人: yswenli
 12 *电子邮箱:wenguoli_520@qq.com
 13 *创建时间:2018/4/8 16:46:40
 14 *描述:
 15 *
 16 *=====================================================================
 17 *修改标记
 18 *修改时间:2018/4/8 16:46:40
 19 *修改人: yswenli
 20 *版本号: V1.0.0.0
 21 *描述:
 22 *
 23 *****************************************************************************/
 24 using SAEA.Commom;
 25 using SAEA.Sockets.Interface;
 26 using SAEA.WebAPI.Http.Base;
 27 using SAEA.WebAPI.Mvc;
 28 using System.Collections.Generic;
 29 using System.Net;
 30 using System.Text;
 31 
 32 namespace SAEA.WebAPI.Http
 33 {
 34     public class HttpResponse : BaseHeader
 35     {
 36         public HttpStatusCode Status { get; set; } = HttpStatusCode.OK;
 37 
 38         public byte[] Content { get; private set; }
 39 
 40 
 41 
 42         internal HttpServer HttpServer { get; set; }
 43 
 44         internal IUserToken UserToken { get; set; }
 45         /// <summary>
 46         /// 创建一个HttpRequest实例
 47         /// </summary>
 48         /// <param name="httpServer"></param>
 49         /// <param name="userToken"></param>
 50         /// <param name="stream"></param>
 51         /// <returns></returns>
 52         internal static HttpResponse CreateInstance(HttpServer httpServer, IUserToken userToken)
 53         {
 54             HttpResponse httpResponse = new HttpResponse("");
 55             httpResponse.HttpServer = httpServer;
 56             httpResponse.UserToken = userToken;
 57             return httpResponse;
 58         }
 59 
 60         /// <summary>
 61         /// 设置回复内容
 62         /// </summary>
 63         /// <param name="httpResponse"></param>
 64         /// <param name="result"></param>
 65         internal static void SetResult(HttpResponse httpResponse, ActionResult result)
 66         {
 67             httpResponse.Content_Encoding = result.ContentEncoding.EncodingName;
 68             httpResponse.Content_Type = result.ContentType;
 69             httpResponse.Status = result.Status;
 70 
 71             if (result is EmptyResult)
 72             {
 73                 return;
 74             }
 75 
 76             if (result is FileResult)
 77             {
 78                 var f = result as FileResult;
 79 
 80                 httpResponse.SetContent(f.Content);
 81 
 82                 return;
 83             }
 84 
 85             httpResponse.SetContent(result.Content);
 86         }
 87 
 88 
 89         public HttpResponse(string content) : this(content, "utf-8", "application/json; charset=utf-8", HttpStatusCode.OK)
 90         {
 91 
 92         }
 93 
 94         public HttpResponse(string content, string encoding, string contentType, HttpStatusCode status)
 95         {
 96             this.Content_Encoding = encoding;
 97             this.Content_Type = contentType;
 98             this.Status = status;
 99             this.SetContent(content);
100         }
101 
102         internal HttpResponse SetContent(byte[] content, Encoding encoding = null)
103         {
104             this.Content = content;
105             this.Encoding = encoding != null ? encoding : Encoding.UTF8;
106             this.Content_Length = content.Length.ToString();
107             return this;
108         }
109 
110         internal HttpResponse SetContent(string content, Encoding encoding = null)
111         {
112             //初始化内容
113             encoding = encoding != null ? encoding : Encoding.UTF8;
114             return SetContent(encoding.GetBytes(content), encoding);
115         }
116 
117 
118         public string GetHeader(ResponseHeaderType header)
119         {
120             return base.GetHeader(header);
121         }
122 
123         public void SetHeader(ResponseHeaderType header, string value)
124         {
125             base.SetHeader(header, value);
126         }
127 
128         /// <summary>
129         /// 构建响应头部
130         /// </summary>
131         /// <returns></returns>
132         protected string BuildHeader()
133         {
134             StringBuilder builder = new StringBuilder();
135             builder.Append(Protocols + SPACE + Status.ToNVString() + ENTER);
136             builder.AppendLine("Server: Wenli's Server");
137             builder.AppendLine("Keep-Alive: timeout=20");
138             builder.AppendLine("Date: " + DateTimeHelper.Now.ToFString("r"));
139 
140             if (!string.IsNullOrEmpty(this.Content_Type))
141                 builder.AppendLine("Content-Type:" + this.Content_Type);
142 
143             //支持跨域
144             builder.AppendLine("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
145             builder.AppendLine("Access-Control-Allow-Origin: *");
146             builder.AppendLine("Access-Control-Allow-Headers: Content-Type,X-Requested-With,Accept,yswenli");//可自行增加额外的header
147             builder.AppendLine("Access-Control-Request-Methods: GET, POST, PUT, DELETE, OPTIONS");
148 
149             if (this.Headers != null && this.Headers.Count > 0)
150             {
151                 foreach (var key in Headers.Names)
152                 {
153                     builder.AppendLine($"{key}: {Headers[key]}");
154                 }
155             }
156 
157             return builder.ToString();
158         }
159 
160         /// <summary>
161         /// 生成数据
162         /// </summary>
163         private byte[] ToBytes()
164         {
165             List<byte> list = new List<byte>();
166             //发送响应头
167             var header = BuildHeader();
168             byte[] headerBytes = this.Encoding.GetBytes(header);
169             list.AddRange(headerBytes);
170 
171             //发送空行
172             byte[] lineBytes = this.Encoding.GetBytes(System.Environment.NewLine);
173             list.AddRange(lineBytes);
174 
175             //发送内容
176             list.AddRange(Content);
177 
178             return list.ToArray();
179         }
180 
181 
182         public void Write(string str)
183         {
184             SetContent(str);
185         }
186 
187         public void BinaryWrite(byte[] data)
188         {
189             SetContent(data);
190         }
191 
192         public void Clear()
193         {
194             this.Write("");
195         }
196 
197         public void End()
198         {
199             HttpServer.Replay(UserToken, this.ToBytes());
200             HttpServer.Close(UserToken);
201         }
202 
203 
204 
205     }
206 }
View Code

  四、HttpServer:这个就是承载webapi的容器;有人说不是有IIS和Apache么?本人想说的是:有self-host方便么?有无需安装,无需配置、随便高性能开跑好么?asp.net core里面都有了这个,没这个就没有逼格....(此处省略一万字),前面还研究tcp、http这个当然不能少了

 1 /****************************************************************************
 2 *Copyright (c) 2018 Microsoft All Rights Reserved.
 3 *CLR版本: 4.0.30319.42000
 4 *机器名称:WENLI-PC
 5 *公司名称:Microsoft
 6 *命名空间:SAEA.WebAPI.Http
 7 *文件名: HttpServer
 8 *版本号: V1.0.0.0
 9 *唯一标识:914acb72-d4c4-4fa1-8e80-ce2f83bd06f0
10 *当前的用户域:WENLI-PC
11 *创建人: yswenli
12 *电子邮箱:wenguoli_520@qq.com
13 *创建时间:2018/4/10 13:51:50
14 *描述:
15 *
16 *=====================================================================
17 *修改标记
18 *修改时间:2018/4/10 13:51:50
19 *修改人: yswenli
20 *版本号: V1.0.0.0
21 *描述:
22 *
23 *****************************************************************************/
24 using SAEA.Sockets.Interface;
25 using SAEA.WebAPI.Common;
26 using SAEA.WebAPI.Http.Net;
27 using System;
28 using System.Collections.Generic;
29 using System.IO;
30 using System.Text;
31 
32 namespace SAEA.WebAPI.Http
33 {
34     class HttpServer
35     {
36         ServerSocket _serverSocket;
37 
38         public HttpServer()
39         {
40             _serverSocket = new ServerSocket();
41             _serverSocket.OnRequested += _serverSocket_OnRequested;
42         }
43 
44         public void Start(int port = 39654)
45         {
46             _serverSocket.Start(port);
47         }
48 
49 
50         private void _serverSocket_OnRequested(IUserToken userToken, string htmlStr)
51         {
52             var httpContext = HttpContext.CreateInstance(this, userToken, htmlStr);
53 
54             var response = httpContext.Response;
55 
56             response.End();
57         }
58 
59         internal void Replay(IUserToken userToken, byte[] data)
60         {
61             _serverSocket.Reply(userToken, data);
62         }
63 
64         internal void Close(IUserToken userToken)
65         {
66             _serverSocket.Disconnected(userToken);
67         }
68 
69 
70     }
71 }

   五、Controller:为了实现类似于mvc的效果,代码量与实现都是其次,但是Controller这个大名鼎鼎的当然不能少了

 1 /****************************************************************************
 2 *Copyright (c) 2018 Microsoft All Rights Reserved.
 3 *CLR版本: 4.0.30319.42000
 4 *机器名称:WENLI-PC
 5 *公司名称:Microsoft
 6 *命名空间:SAEA.WebAPI.Mvc
 7 *文件名: Controller
 8 *版本号: V1.0.0.0
 9 *唯一标识:a303db7d-f83c-4c49-9804-032ec2236232
10 *当前的用户域:WENLI-PC
11 *创建人: yswenli
12 *电子邮箱:wenguoli_520@qq.com
13 *创建时间:2018/4/10 13:58:08
14 *描述:
15 *
16 *=====================================================================
17 *修改标记
18 *修改时间:2018/4/10 13:58:08
19 *修改人: yswenli
20 *版本号: V1.0.0.0
21 *描述:
22 *
23 *****************************************************************************/
24 
25 using SAEA.WebAPI.Http;
26 
27 namespace SAEA.WebAPI.Mvc
28 {
29     /// <summary>
30     /// WebApi控制器
31     /// </summary>
32     public abstract class Controller
33     {
34         public HttpContext HttpContext { get; set; }
35 
36         /// <summary>
37         /// 返回Json
38         /// </summary>
39         /// <param name="data"></param>
40         /// <returns></returns>
41         protected JsonResult Json(object data)
42         {
43             return new JsonResult(data);
44         }
45         /// <summary>
46         /// 自定义内容
47         /// </summary>
48         /// <param name="data"></param>
49         /// <returns></returns>
50         protected ContentResult Content(string data)
51         {
52             return new ContentResult(data);
53         }
54 
55 
56         /// <summary>
57         /// 小文件
58         /// </summary>
59         /// <param name="filePath"></param>
60         /// <returns></returns>
61         protected FileResult File(string filePath)
62         {
63             return new FileResult(filePath);
64         }
65 
66         /// <summary>
67         /// 空结果
68         /// </summary>
69         /// <returns></returns>
70         protected EmptyResult Empty()
71         {
72             return new EmptyResult();
73         }
74     }
75 }

  六、ActionResult:是mvc里面针对reponse结果进行了一个http格式的封装,本人主要实现了ContentResult、JsonResult、FileResult三个,至于其他的在WebAPI里基本上用不到,所以还是以JsonResult为主

 1 /****************************************************************************
 2 *Copyright (c) 2018 Microsoft All Rights Reserved.
 3 *CLR版本: 4.0.30319.42000
 4 *机器名称:WENLI-PC
 5 *公司名称:Microsoft
 6 *命名空间:SAEA.WebAPI.Mvc
 7 *文件名: JsonResult
 8 *版本号: V1.0.0.0
 9 *唯一标识:340c3ef0-2e98-4f25-998f-2bb369fa2794
10 *当前的用户域:WENLI-PC
11 *创建人: yswenli
12 *电子邮箱:wenguoli_520@qq.com
13 *创建时间:2018/4/10 16:48:06
14 *描述:
15 *
16 *=====================================================================
17 *修改标记
18 *修改时间:2018/4/10 16:48:06
19 *修改人: yswenli
20 *版本号: V1.0.0.0
21 *描述:
22 *
23 *****************************************************************************/
24 using SAEA.WebAPI.Common;
25 using System;
26 using System.Collections.Generic;
27 using System.Net;
28 using System.Text;
29 
30 namespace SAEA.WebAPI.Mvc
31 {
32     public class JsonResult : ActionResult
33     {
34         public JsonResult(object model) : this(SerializeHelper.Serialize(model))
35         {
36 
37         }
38         public JsonResult(string json) : this(json, Encoding.UTF8)
39         {
40 
41         }
42 
43         public JsonResult(string json, HttpStatusCode status)
44         {
45             this.Content = json;
46             this.ContentEncoding = Encoding.UTF8;
47             this.ContentType = "application/json; charset=utf-8";
48             this.Status = status;
49         }
50 
51         public JsonResult(string json, Encoding encoding, string contentType = "application/json; charset=utf-8")
52         {
53             this.Content = json;
54             this.ContentEncoding = encoding;
55             this.ContentType = contentType;
56         }
57     }
58 }

  七、RouteTable:MVC里面有一个相当重要的概念叫约定优先,即为Controller、Action的名称是按某种规则来写编码的,其中将URL与自定义Controller对应起来的缓存映射就是RouteTable,并且作为缓存,也能极大的提升访问性能。当然这里并没有严格按照asp.net mvc里面的routetable来设计,而是根据只是实现webapi,并使用缓存提升性能来实现的

  1 /****************************************************************************
  2 *Copyright (c) 2018 Microsoft All Rights Reserved.
  3 *CLR版本: 4.0.30319.42000
  4 *机器名称:WENLI-PC
  5 *公司名称:Microsoft
  6 *命名空间:SAEA.WebAPI.Mvc
  7 *文件名: RouteTable
  8 *版本号: V1.0.0.0
  9 *唯一标识:1ed5d381-d7ce-4ea3-b8b5-c32f581ad49f
 10 *当前的用户域:WENLI-PC
 11 *创建人: yswenli
 12 *电子邮箱:wenguoli_520@qq.com
 13 *创建时间:2018/4/12 10:55:31
 14 *描述:
 15 *
 16 *=====================================================================
 17 *修改标记
 18 *修改时间:2018/4/12 10:55:31
 19 *修改人: yswenli
 20 *版本号: V1.0.0.0
 21 *描述:
 22 *
 23 *****************************************************************************/
 24 using System;
 25 using System.Collections.Generic;
 26 using System.Linq;
 27 using System.Reflection;
 28 using System.Text;
 29 
 30 namespace SAEA.WebAPI.Mvc
 31 {
 32     /// <summary>
 33     /// SAEA.WebAPI路由表
 34     /// </summary>
 35     public static class RouteTable
 36     {
 37         static object _locker = new object();
 38 
 39         static List<Routing> _list = new List<Routing>();
 40 
 41 
 42         /// <summary>
 43         /// 获取routing中的缓存
 44         /// 若不存在则创建
 45         /// </summary>
 46         /// <param name="controllerType"></param>
 47         /// <param name="controllerName"></param>
 48         /// <param name="actionName"></param>
 49         /// <param name="isPost"></param>
 50         /// <returns></returns>
 51         public static Routing TryGet(Type controllerType, string controllerName, string actionName, bool isPost)
 52         {
 53             lock (_locker)
 54             {
 55                 var list = _list.Where(b => b.ControllerName.ToLower() == controllerName.ToLower() && b.ActionName.ToLower() == actionName.ToLower() && b.IsPost == isPost).ToList();
 56 
 57                 if (list == null || list.Count == 0)
 58                 {
 59                     var routing = new Routing()
 60                     {
 61                         ControllerName = controllerName,
 62                         ActionName = actionName,
 63                         IsPost = isPost
 64                     };
 65 
 66                     var actions = controllerType.GetMethods().Where(b => b.Name.ToLower() == actionName.ToLower()).ToList();
 67 
 68                     if (actions == null || actions.Count == 0)
 69                     {
 70                         throw new Exception($"{controllerName}/{actionName}找不到此action!");
 71                     }
 72                     else if (actions.Count > 2)
 73                     {
 74                         throw new Exception($"{controllerName}/{actionName}有多个重复的的action!");
 75                     }
 76                     else
 77                     {                        
 78                         routing.Instance = System.Activator.CreateInstance(controllerType);
 79 
 80                         //类上面的过滤
 81                         var attrs = controllerType.GetCustomAttributes(true);
 82 
 83                         if (attrs != null)
 84                         {
 85                             var attr = attrs.Where(b => b.GetType().BaseType.Name == "ActionFilterAttribute").FirstOrDefault();
 86 
 87                             routing.Atrr = attr;
 88 
 89                         }
 90                         else
 91                         {
 92                             routing.Atrr = null;
 93                         }
 94 
 95                         routing.Action = actions[0];
 96 
 97                         //action上面的过滤
 98                         if (routing.Atrr == null)
 99                         {
100                             attrs = actions[0].GetCustomAttributes(true);
101 
102                             if (attrs != null)
103                             {
104                                 var attr = attrs.Where(b => b.GetType().BaseType.Name == "ActionFilterAttribute").FirstOrDefault();
105 
106                                 routing.Atrr = attr;
107 
108                             }
109                             else
110                             {
111                                 routing.Atrr = null;
112                             }
113                         }
114                     }
115                     _list.Add(routing);
116                     return routing;
117                 }
118                 else if (list.Count > 1)
119                 {
120                     throw new Exception("500");
121                 }
122                 return list.FirstOrDefault();
123             }
124         }
125     }
126 
127 }

  在MVC的思想里面ActionFilterAtrribute的这个AOP设计也一直伴随左右,比如记日志、黑名单、权限、验证、限流等等功能,所以路由的时候也会缓存这个。至此一些关键性的地方都已经弄的差不多了,为了更好的了解上面说的这些,下面是vs2017中项目的结构截图:

  接下来就是按asp.net mvc命名方式,写个测试webapi看看情况,首先还是测试项目结构图:

  program里面启动一下服务:

1 MvcApplication mvcApplication = new MvcApplication();
2 
3 mvcApplication.Start();

  HomeController里面按asp.net mvc的习惯来编写代码:

 1 /****************************************************************************
 2 *Copyright (c) 2018 Microsoft All Rights Reserved.
 3 *CLR版本: 4.0.30319.42000
 4 *机器名称:WENLI-PC
 5 *公司名称:Microsoft
 6 *命名空间:SAEA.WebAPITest.Controllers
 7 *文件名: HomeController
 8 *版本号: V1.0.0.0
 9 *唯一标识:e00bb57f-e3ee-4efe-a7cf-f23db767c1d0
10 *当前的用户域:WENLI-PC
11 *创建人: yswenli
12 *电子邮箱:wenguoli_520@qq.com
13 *创建时间:2018/4/10 16:43:26
14 *描述:
15 *
16 *=====================================================================
17 *修改标记
18 *修改时间:2018/4/10 16:43:26
19 *修改人: yswenli
20 *版本号: V1.0.0.0
21 *描述:
22 *
23 *****************************************************************************/
24 using SAEA.WebAPI.Mvc;
25 using SAEA.WebAPITest.Attrubutes;
26 
27 namespace SAEA.WebAPITest.Controllers
28 {
29     [LogAtrribute]
30     public class HomeController : Controller
31     {
32         public ActionResult Index()
33         {
34             return Content("Hello,I'm SAEA.WebAPI!");
35         }
36 
37         public ActionResult Get(int id)
38         {
39             return Json(new { Name = "yswenli", Sex = "" });
40         }
41 
42         public ActionResult Show()
43         {
44             var response = HttpContext.Response;
45 
46             response.Content_Type = "text/html; charset=utf-8";
47 
48             response.Write("<h3>测试一下那个response对象使用情况!</h3>参考消息网4月12日报道外媒称,法国一架“幻影-2000”");            
49 
50             response.End();
51 
52             return Empty();
53         }
54 
55         public ActionResult Download()
56         {
57             return File(HttpContext.Server.MapPath("/Content/Image/c984b2fb80aeca7b15eda8c004f2e0d4.jpg"));
58         }
59     }
60 }

  增加一个LogAtrribute打印一些内容:

 1 /****************************************************************************
 2 *Copyright (c) 2018 Microsoft All Rights Reserved.
 3 *CLR版本: 4.0.30319.42000
 4 *机器名称:WENLI-PC
 5 *公司名称:Microsoft
 6 *命名空间:SAEA.WebAPITest.Common
 7 *文件名: LogAtrribute
 8 *版本号: V1.0.0.0
 9 *唯一标识:2a261731-b8f6-47de-b2e4-aecf2e0e0c0f
10 *当前的用户域:WENLI-PC
11 *创建人: yswenli
12 *电子邮箱:wenguoli_520@qq.com
13 *创建时间:2018/4/11 13:46:42
14 *描述:
15 *
16 *=====================================================================
17 *修改标记
18 *修改时间:2018/4/11 13:46:42
19 *修改人: yswenli
20 *版本号: V1.0.0.0
21 *描述:
22 *
23 *****************************************************************************/
24 using SAEA.Commom;
25 using SAEA.WebAPI.Http;
26 using SAEA.WebAPI.Mvc;
27 
28 namespace SAEA.WebAPITest.Attrubutes
29 {
30     public class LogAtrribute : ActionFilterAttribute
31     {
32         /// <summary>
33         /// 执行前
34         /// </summary>
35         /// <param name="httpContext"></param>
36         /// <returns>返回值true为继续,false为终止</returns>
37         public override bool OnActionExecuting(HttpContext httpContext)
38         {
39             return true;
40         }
41 
42         /// <summary>
43         /// 执行后
44         /// </summary>
45         /// <param name="httpContext"></param>
46         /// <param name="result"></param>
47         public override void OnActionExecuted(HttpContext httpContext, ActionResult result)
48         {
49             ConsoleHelper.WriteLine($"请求地址:{httpContext.Request.Query},回复内容:{result.Content}");
50         }
51     }
52 }

  最后F5跑起来看看效果:

  使用Apache ab.exe压测一下性能如何:

  至此,一个简洁、高效的WebApi就初步完成了!

 

 

转载请标明本文来源:http://www.zjblrv.net/yswenli/p/8858669.html
更多内容欢迎star作者的github:https://github.com/yswenli/SAEA
如果发现本文有什么问题和任何建议,也随时欢迎交流~

 

posted @ 2018-04-16 22:15 yswenli 阅读(...) 评论(...) 编辑 收藏