autofac + owin + webform + mvc + webapi集成demo
http://git.oschina.net/shiningrise/AutofacOwinDemo
using Microsoft.Owin; using Owin; using System.Web.Mvc; using Autofac; using Autofac.Integration.Owin; using Autofac.Integration.Mvc; using Autofac.Integration.WebApi; using System.Web.Http; using System.Reflection; using System.Web.Routing; [assembly: OwinStartupAttribute(typeof(AutofacOwinDemo.Startup))] namespace AutofacOwinDemo { public partial class Startup { public void Configuration(IAppBuilder app) { //app.Run(context => //{ // context.Response.ContentType = "text/plain"; // return context.Response.WriteAsync("Hello World!"); //}); ConfigureAuth(app); var builder = new ContainerBuilder(); builder.RegisterType<Test1>().As<ITest>(); builder.RegisterType<Test2>(); builder.RegisterControllers(typeof(MvcApplication).Assembly); var config = new HttpConfiguration(); WebApiConfig.Register(config); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterWebApiFilterProvider(config); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); app.UseAutofacMiddleware(container); app.UseAutofacMvc(); app.UseAutofacWebApi(config); app.UseWebApi(config); } } }
webform属性注入
pagebase方式
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Owin; using Autofac; using Autofac.Integration.Owin; using System.Web; namespace AutofacOwinDemo { public partial class WebForm1 : System.Web.UI.Page { public ITest Test { get; set; } protected void Page_PreInit(object sender, EventArgs e) { var owin = this.Context.Request.GetOwinContext() ; var scop = owin.GetAutofacLifetimeScope(); scop.InjectProperties(this); } protected void Page_Load(object sender, EventArgs e) { } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; using System.Web.Security; using System.Web.SessionState; namespace AutofacOwinDemo { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); //GlobalConfiguration.Configure(WebApiConfig.Register); //这里要删除,让owin完全管理webapi FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章