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

Autofac 同时支持MVC 与Webapi

时间:2016-05-18 14:36:36      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:

1.引用

using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;

2.在Global中的Application_Start方法中添加如下
      // Get your HttpConfiguration.
            var config = GlobalConfiguration.Configuration;

            var builder = new ContainerBuilder();
            //builder.RegisterType<Helper>().As<IHelper>().InstancePerLifetimeScope();

            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

            // OPTIONAL: Register the Autofac filter provider.
            builder.RegisterWebApiFilterProvider(config);
            var container = builder.Build();
            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

 

Autofac 同时支持MVC 与Webapi

标签:

原文地址:http://www.cnblogs.com/wuxfly6/p/5504990.html

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