码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-06-08 08:04:12      阅读:1189      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/shiningrise/p/5568901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!