none
在MVC2上實作AutoFac IOC模型問題 RRS feed

  • 問題

  • 最近在實作MVC2上面實作AutoFac這個作法遇到一個很怪的情形,Routine物件不正長,不知道大家有沒有遇到

    當我在Global.asax裡面的實作

            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                routes.MapRoute(
                    "Default", // 路由名称
                    "{controller}/{action}/{id}", // 带有参数的 URL
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
                );

            }

            private void SetupResolveRules(ContainerBuilder builder)
            {
                builder.RegisterType<KMFacade>().As<SuperKM.Biz.IKMFacade>();
            }

            static IContainerProvider _containerProvider;

            public IContainerProvider ContainerProvider
            {
                get { return _containerProvider; }
            }

            protected void Application_Start()
            {

                var builder = new ContainerBuilder();

                SetupResolveRules(builder);


                _containerProvider = new ContainerProvider(builder.Build());

                ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));

               
                AreaRegistration.RegisterAllAreas();

                RegisterRoutes(RouteTable.Routes);

    }

    Application_Start 註冊以下此段物件時,Routine卻無法按照原來的Routine路徑去跑,當我把以下此段拿掉後又是正常.

                var builder = new ContainerBuilder();

                SetupResolveRules(builder);


                _containerProvider = new ContainerProvider(builder.Build());

                ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));

    2010年12月28日 上午 09:39

解答

  • 最近在實作MVC2上面實作AutoFac這個作法遇到一個很怪的情形,Routine物件不正長,不知道大家有沒有遇到

    當我在Global.asax裡面的實作

            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                routes.MapRoute(
                    "Default", // 路由名称
                    "{controller}/{action}/{id}", // 带有参数的 URL
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
                );

            }

            private void SetupResolveRules(ContainerBuilder builder)
            {
                builder.RegisterType<KMFacade>().As<SuperKM.Biz.IKMFacade>();
            }

            static IContainerProvider _containerProvider;

            public IContainerProvider ContainerProvider
            {
                get { return _containerProvider; }
            }

            protected void Application_Start()
            {

                var builder = new ContainerBuilder();

                SetupResolveRules(builder);


                _containerProvider = new ContainerProvider(builder.Build());

                ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));

               
                AreaRegistration.RegisterAllAreas();

                RegisterRoutes(RouteTable.Routes);

    }

    Application_Start 註冊以下此段物件時,Routine卻無法按照原來的Routine路徑去跑,當我把以下此段拿掉後又是正常.

                var builder = new ContainerBuilder();

                SetupResolveRules(builder);


                _containerProvider = new ContainerProvider(builder.Build());

                ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));

     

     

    我找到到問題了,原來是少一段builder.RegisterControllers(Assembly.GetExecutingAssembly());

     

             protected void Application_Start()
            {

                var builder = new ContainerBuilder();

                SetupResolveRules(builder);

               builder.RegisterControllers(Assembly.GetExecutingAssembly());


                _containerProvider = new ContainerProvider(builder.Build());

                ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));

               
                AreaRegistration.RegisterAllAreas();

                RegisterRoutes(RouteTable.Routes);

    }

    • 已標示為解答 Steventu 2010年12月29日 上午 01:33
    2010年12月29日 上午 01:33