asp.net-mvc – 如何在使用Ninject for DI的MVC应用程序中使用AsyncController?

有谁知道如何在使用Ninject for DI的mvc应用程序中使用AsyncController?

当我不使用ninject时,AsyncController工作正常,但我不能让它们一起工作.

我在我的sitemodule中添加了以下内容,但没有去.

Bind<IAsyncController>( ).To<AsyncController>( ).InSingletonScope( );

很抱歉没有详细说明.

我的控制器看起来像这样

 [HandleError]
    public class HomeController : AsyncController
    {
        public void IndexAsync( )
        {
            AsyncManager.OutstandingOperations.Increment( );

            RssFeed feed = new RssFeed( );
            feed.GetRssFeedAsyncCompleted += ( s, e ) =>
                {
                    AsyncManager.Parameters[ "items" ] = e.Items;
                    AsyncManager.OutstandingOperations.Decrement( );
                };
            feed.GetRssFeedAsync( "http://feeds.abcnews.com/abcnews/topstories" );
        }

        public ActionResult IndexCompleted( IEnumerable<SyndicationItem> items )
        {
            ViewData[ "SyndicationItems" ] = items;
            return View( );
        }
    }

而我的global.asax看起来像这样

public class MvcApplication :  System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
        }
        protected void Application_Start( )
        {
            AreaRegistration.RegisterAllAreas( );
            RegisterRoutes( RouteTable.Routes );
        }
    }

这很好用.但是一旦我使用ninject(ninject 2.0),当我尝试访问索引页面时,我得到404页面未找到错误.这就是我配置ninject的方式

public class MvcApplication : NinjectHttpApplication //System.Web.HttpApplication
    {
        #region IOC
        static IKernel container;
        public static IKernel Container
        {
            get
            {
                if ( container == null ) { container = new StandardKernel( new SiteModule( ) ); }
                return container;
            }
        }

        protected override IKernel CreateKernel( )
        {
            return Container;
        }
        #endregion

        public static void RegisterRoutes( RouteCollection routes )
        {
            routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );
            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
        }

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

        //    RegisterRoutes(RouteTable.Routes);
        //}

        protected override void OnApplicationStarted( )
        {
            AreaRegistration.RegisterAllAreas( );
            RegisterRoutes( RouteTable.Routes );
        }
    }

    public class SiteModule : NinjectModule
    {
        public override void Load( )
        {

        }
    }

我需要绑定我的sitemodule上的任何内容吗?

顺便说一句,我正在使用他在博客Here 中发布的Jeff Prosise的例子
你可以下载他的演示应用程序并尝试Ninject-ify it 🙂

任何帮助赞赏.

最佳答案 它似乎无法正常工作,因为标准的NinjectControllerFactory将NinjectActionInvoker插入到控制器的ActionInvoker属性中. NinjectActionInvoker派生自ControllerActionInvoker.但是,AsyncController使用从AsyncControllerActionInvoker派生的ActionInvokers.由于某种原因,这会导致控制器与路由不匹配,并返回404.

真正的修复将是Ninject的补丁,以支持使用AsyncControllerActionInvokers构建AsyncController.

但是,与此同时,这是一个解决方法:

在您的Global.asax中,添加此覆盖:

    protected override Ninject.Web.Mvc.NinjectControllerFactory CreateControllerFactory()
    {
        return new MyNinjectControllerFactory( kernel );
    }

然后为MyNinjectControllerFactory添加此类:

public class MyNinjectControllerFactory : Ninject.Web.Mvc.NinjectControllerFactory
{
    public MyNinjectControllerFactory( IKernel kernel ) : base( kernel ) { }

    protected override IController GetControllerInstance( RequestContext requestContext, Type controllerType )
    {
        if ( controllerType == null )
        {
            // let the base handle 404 errors with proper culture information
            return base.GetControllerInstance( requestContext, controllerType );
        }

        var controller = Kernel.TryGet( controllerType ) as IController;

        if ( controller == null )
            return base.GetControllerInstance( requestContext, controllerType );

        //var standardController = controller as Controller;
        //if ( standardController != null )
        //    standardController.ActionInvoker = CreateActionInvoker();

        return controller;
    }
}

这是NinjectControllerFactory的副本,它省去了ActionInvoker的赋值.

如果您的代码依赖于注入ActionFilters的依赖项,则需要创建自己的ActionInvoker,它返回使用Ninject的AsyncControllerActionInvoker.查看NinjectActionInvoker的Ninject.Web.Mvc源代码.

HTH

点赞