ASP.NET WEB API 2: HTTP MESSAGE LIFECYLE
https://www.asp.net/media/4071077/aspnet-web-api-poster.pdf
1.You can host Web API inside IIS or inside your own process (self-hosting).
2.The HTTP request message is first converted to an HttpRequestMessage object, which provides strongly typed access to the HTTP message.
3.HttpMessageHandler
3.1 Delegating Handler
3.2 HTTPRoutingDispatcher
3.3 HTTPControllerDispatcher
StepA:Create Api Controller
4.Controller
StepB: Select controller action
4.1 Authentication Filters
4.2 Authorization Filters
StepC:Model Binding
4.3 Action Filters 源码https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Http/Filters/ActionFilterAttribute.cs
StepE: Invoke Action -->这里有可能触发Exception Filters
StepD:Result Conversion
在看pdf的时候,还需要注意标准
实际使用中遇到的问题
使用Autofac进行构造函数注入,但是依赖api传递进来的参数。
之前使用的方案是,在ActionFilter中拿到controller以及api参数,然后手动调用resolve去实例化,本来需要构造函数注入的属性。
调整之后:(必须在DelegateHandler中解析,因为ActionFilter的执行,是在Controller实例化之后)
在DelegateHandler中解析参数,并存储。然后在Autofac的lambda注册的地方,委托那边直接OwinRequestScopeContext拿到参数。
var apiRequest = request.Content.ReadAsAsync<ApiRequest<dynamic>>().Result;
OwinRequestScopeContext.Current.Items["CountryCode"] = apiRequest.Header.OpCo;
扩展阅读
https://exceptionnotfound.net/the-asp-net-web-api-2-http-message-lifecycle-in-43-easy-steps-2/
http://rajeevdotnet.blogspot.com/2018/03/the-lifecycle-of-aspnet-web-api.html
http://www.cnblogs.com/shanyou/archive/2012/03/17/2404180.html
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了