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);
        }
    }
}

 

posted @ 2016-06-08 07:28  shiningrise  阅读(2216)  评论(2编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css