首页 > 其他 > 详细

Autofac使用

时间:2016-06-16 17:37:41      阅读:219      评论:0      收藏:0      [点我收藏+]

1引用dll

 autofac.dll 

 autofac.integration.mvc.dll

技术分享
 protected void Application_Start()
        {
              var builder = RegisterService();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));

        }


   private ContainerBuilder RegisterService()
        {
            var builder = new ContainerBuilder();

            var baseType = typeof(IDependency);
            var assemblys = AppDomain.CurrentDomain.GetAssemblies().ToList();
            var AllServices = assemblys
                .SelectMany(s => s.GetTypes())
                .Where(p => baseType.IsAssignableFrom(p) && p != baseType);

            builder.RegisterControllers(assemblys.ToArray());

            builder.RegisterAssemblyTypes(assemblys.ToArray())
                   .Where(t => baseType.IsAssignableFrom(t) && t != baseType)
                   .AsImplementedInterfaces().InstancePerLifetimeScope();
            return builder;
        }
2global.asax.cs里配置

3控制器里使用的例子

技术分享
IUserService userService;
public HomeController(IUserService _userSerivce)
{
    userService=_userServicve;
}
 
例子

 

Autofac使用

原文:http://www.cnblogs.com/kuiyu/p/5591434.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!