asp.net mvc - how to use AsyncController in MVC application using Ninject for DI? -
does know how use asynccontroller in mvc application uses ninject di?
asynccontroller works fine when dont use ninject cant make them work together.
i added following in sitemodule no go.
bind<iasynccontroller>( ).to<asynccontroller>( ).insingletonscope( );
sorry not explaining in details.
my controller looks this
[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( ); } }
and global.asax looks this
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 parameters new { controller = "home", action = "index", id = "" } // parameter defaults ); } protected void application_start( ) { arearegistration.registerallareas( ); registerroutes( routetable.routes ); } }
this works fine. use ninject (ninject 2.0 ) 404 page not found error when try access index page. how configuring ninject
public class mvcapplication : ninjecthttpapplication //system.web.httpapplication { #region ioc static ikernel container; public static ikernel container { { 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 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( ) { } }
do need bind on sitemodule?
btw using jeff prosise's example posted in blog here can download demo application , try ninject-ify :)
any appreciated.
it appears it's not working because standard ninjectcontrollerfactory inserts ninjectactioninvoker controller's actioninvoker property. ninjectactioninvoker derived controlleractioninvoker
. asynccontroller, however, uses actioninvokers derived asynccontrolleractioninvoker
. reason, causes controller not match route, , returns 404.
the real fix patch ninject support construction of asynccontroller asynccontrolleractioninvokers.
however, in meantime, here workaround:
in global.asax, add override:
protected override ninject.web.mvc.ninjectcontrollerfactory createcontrollerfactory() { return new myninjectcontrollerfactory( kernel ); }
and add class 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 base handle 404 errors proper culture information return base.getcontrollerinstance( requestcontext, controllertype ); } var controller = kernel.tryget( controllertype ) icontroller; if ( controller == null ) return base.getcontrollerinstance( requestcontext, controllertype ); //var standardcontroller = controller controller; //if ( standardcontroller != null ) // standardcontroller.actioninvoker = createactioninvoker(); return controller; } }
this copy of ninjectcontrollerfactory leaves out assignment of actioninvoker.
if have code depends on dependencies being injected actionfilters, need create own actioninvoker returns asynccontrolleractioninvoker uses ninject. @ ninject.web.mvc source ninjectactioninvoker.
hth
Comments
Post a Comment