1. 程式人生 > >Autofac MVC 依賴註入

Autofac MVC 依賴註入

bsp AS res end inf builder har cde sem

今天在這裏分享一哈Autofac mvc 依賴註入單個註入的代碼

       /// <summary>
        /// 初始化容器
        /// </summary>
        public static void InitAutofc()
        {
            ContainerBuilder builder = new ContainerBuilder();
            //調用需要註冊的類
            SetupResolveRules(builder);
            //註冊控制器
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            IContainer container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            AreaRegistration.RegisterAllAreas();
        }
        /// <summary>
        ///  需要用到的類進行註冊
        /// </summary>
        /// <param name="builder">容器</param>
        private static void SetupResolveRules(ContainerBuilder builder)
        {
            //用戶信息
            builder.RegisterType<TUSERINFODao>().As<ITUSERINFODao>();
            builder.RegisterType<TUSERINFOService>().As<ITUSERINFOService>();

        }

  

Autofac MVC 依賴註入