c# - Dependency Injection with ASP.NET Web API -


i trying use di in asp.net web api controller. apparently there few implementations out there. wondering of these (castle, ninject, unity etc.) easiest configure/maintain in combination asp.net web api? getting error:

di.controllers.importjsoncontroller' not have default constructor.

if add empty constructor, constructor ifilter ignored.

this controller:

public class importjsoncontroller : apicontroller {     private readonly ifilter _filter;      public importjsoncontroller(ifilter filter)     {         _filter = filter;     }      public httpresponsemessage post([frombody]dynamic value)     {         //do         return response;     } } 

you don't need di container this. here's how hand:

public class poormanscompositionroot : ihttpcontrolleractivator {     public ihttpcontroller create(         httprequestmessage request,         httpcontrollerdescriptor controllerdescriptor,         type controllertype)     {         if (controllertype == typeof(importjsoncontroller))             return new importjsoncontroller(new myfilter());          return null;     } } 

you need tell asp.net web api class (e.g. in global.asax):

globalconfiguration.configuration.services.replace(     typeof(ihttpcontrolleractivator),     new poormanscompositionroot()); 

you can read details here: http://blog.ploeh.dk/2012/09/28/dependencyinjectionandlifetimemanagementwithasp.netwebapi


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -