integration asp.net web api with autofac and owin
There is an example project showing Web API in conjunction with OWIN self hosting
https://github.com/autofac/Examples/tree/master/src/WebApiExample.OwinSelfHost
https://github.com/autofac/Examples/blob/master/src/WebApiExample.OwinSelfHost/Startup.cs
Owin
https://autofaccn.readthedocs.io/en/latest/integration/owin.html
OWIN (Open Web Interface for .NET) is a simpler model for composing web-based applications without tying the application to the web server. To do this, a concept of “middleware” is used to create a pipeline through which requests travel.
Due to the differences in the way OWIN handles the application pipeline (detecting when a request starts/ends, etc.) integrating Autofac into an OWIN application is slightly different than the way it gets integrated into more “standard” ASP.NET apps. You can read about OWIN and how it works on this overview.
The important thing to remember is that order of OWIN middleware registration matters. Middleware gets processed in order of registration, like a chain, so you need to register foundational things (like Autofac middleware) first.
Quick Start
To take advantage of Autofac in your OWIN pipeline:
- Reference the
Autofac.Owin
package from NuGet. - Build your Autofac container.
- Register the Autofac middleware with OWIN and pass it the container.
Web api integration with owin
https://autofaccn.readthedocs.io/en/latest/integration/webapi.html#owin-integration
If you are using Web API as part of an OWIN application, you need to:
- Do all the stuff for standard Web API integration - register controllers, set the dependency resolver, etc.
- Set up your app with the base Autofac OWIN integration.
- Add a reference to the Autofac.WebApi2.Owin NuGet package.
- In your application startup class, register the Autofac Web API middleware after registering the base Autofac middleware.
A common error in OWIN integration is use of the GlobalConfiguration.Configuration
.
In OWIN you create the configuration from scratch.
You should not reference GlobalConfiguration.Configuration
anywhere when using the OWIN integration.
作者: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:你的「微服务管家」又秀新绝活了
2018-01-29 NUnit Console Command Line