autofac + owin + webform + mvc + webapi集成demo

Posted 吴晓阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        }
    }
}

 

以上是关于autofac + owin + webform + mvc + webapi集成demo的主要内容,如果未能解决你的问题,请参考以下文章

Autofac.Integration.Owin

Autofac.Integration.Mvc.Owin分析

ASP.NET MVC IOC依赖注入之Autofac系列- WebForm当中应用

OWIN support for the Web API 2 and MVC 5 integrations in Autofac

Autofac 错误:无法加载文件或程序集 'System.Web.Http, Version=5.2.0.0,...' 我的项目是 Owin WebApi2 SelfHost

使用Autofac动态注入启动Api服务