Autofac构建

1.初始化

using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System;
using System.IO;
using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc; 

public static IContainer RegisterIOC()
        {
            ContainerBuilder builder = new ContainerBuilder();
            //构造函数注入
            builder.RegisterAssemblyTypes(Assembly.GetCallingAssembly()).AsImplementedInterfaces();
            //注入控制器并实现属性注入
            builder.RegisterControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();
            //支持过滤器的属性注入
            builder.RegisterFilterProvider();

builder.RegisterType<AccountService>().As<IAccountService>();


       var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            return container;
}

 

2.在global.asax.cs

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            
            Func<ILifetimeScope, ILifetimeScope> scopeProvider = (ILifetimeScope container) =>
            {
                if (HttpContext.Current.Items[typeof(ILifetimeScope)] == null)
                {
                    HttpContext.Current.Items[typeof(ILifetimeScope)] = container.BeginLifetimeScope("AutofacWebRequest");
                }

                return (ILifetimeScope)HttpContext.Current.Items[typeof(ILifetimeScope)];
            };

scopeProvider (RegisterIOC())
}

 

3.手工获取

 public class BeanFactory
    {
        /// <summary>
        /// 获取Bean
        /// </summary>
        /// <typeparam name="T">获取的实例类型</typeparam>
        /// <returns>获取的实例</returns>
        public static T GetBean<T>()
        {
            try
            {
                if (System.Web.HttpContext.Current != null)
                {
                    ILifetimeScope scope = System.Web.HttpContext.Current.Items[typeof(ILifetimeScope)] as ILifetimeScope;
                    if (scope != null)
                    {
                        return scope.Resolve<T>();
                    }
                }
            }
            catch
            {
            }

            return ServiceLocator.Current.GetInstance<T>();
        }

        /// <summary>
        /// 获取Bean
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object GetBean(System.Type type) 
        {
            try
            {
                if (System.Web.HttpContext.Current != null)
                {
                    ILifetimeScope scope = System.Web.HttpContext.Current.Items[typeof(ILifetimeScope)] as ILifetimeScope;
                    if (scope != null)
                    {
                        return scope.Resolve(type);
                    }
                }
            }
            catch
            {
            }

            return ServiceLocator.Current.GetInstance (type) ;
        }
    }

 

4.参考 http://www.360doc.com/content/14/0620/09/10504424_388250715.shtml

 

转载于:https://www.cnblogs.com/zhshlimi/p/8023708.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值