Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET Web API binding with ninject
    text
    copied!<p>I have just installed the mvc4 rc update and I am trying to build an api application with little luck.</p> <p>I am using ninject but cant get my controllers to load. I keep getting an error</p> <blockquote> <p>Type 'Api.Controllers.ConsumerController' does not have a default constructor</p> </blockquote> <p>I am very new to mvc and using injection so please bear with me.</p> <p>I havent done anything special to the default binding that is created via nuget</p> <pre><code> public static class NinjectWebCommon { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); /// &lt;summary&gt; /// Starts the application /// &lt;/summary&gt; public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } /// &lt;summary&gt; /// Stops the application. /// &lt;/summary&gt; public static void Stop() { bootstrapper.ShutDown(); } /// &lt;summary&gt; /// Creates the kernel that will manage your application. /// &lt;/summary&gt; /// &lt;returns&gt;The created kernel.&lt;/returns&gt; private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind&lt;Func&lt;IKernel&gt;&gt;().ToMethod(ctx =&gt; () =&gt; new Bootstrapper().Kernel); kernel.Bind&lt;IHttpModule&gt;().To&lt;HttpApplicationInitializationHttpModule&gt;(); RegisterServices(kernel); return kernel; } /// &lt;summary&gt; /// Load your modules or register your services here! /// &lt;/summary&gt; /// &lt;param name="kernel"&gt;The kernel.&lt;/param&gt; private static void RegisterServices(IKernel kernel) { kernel.Bind&lt;IConsumerRepository&gt;().To&lt;ConsumerRepository&gt;(); } } </code></pre> <p>My controller looks like</p> <pre><code> private readonly IConsumerRepository _repository; public ConsumerController(IConsumerRepository repository) { _repository = repository; } [HttpGet] public IQueryable&lt;Consumer&gt; Get(Guid id) { return _repository.Get(id).AsQueryable(); } </code></pre> <p>What do I need to do to get the api controllers to work with ninject?</p> <p>Sorry if this is simple stuff</p> <p>I tried your suggestion Michael however after changing the the webcommon.cs to this</p> <pre><code> private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind&lt;Func&lt;IKernel&gt;&gt;().ToMethod(ctx =&gt; () =&gt; new Bootstrapper().Kernel); kernel.Bind&lt;IHttpModule&gt;().To&lt;HttpApplicationInitializationHttpModule&gt;(); RegisterServices(kernel); GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel); return kernel; } /// &lt;summary&gt; /// Load your modules or register your services here! /// &lt;/summary&gt; /// &lt;param name="kernel"&gt;The kernel.&lt;/param&gt; private static void RegisterServices(IKernel kernel) { kernel.Bind&lt;IConsumerRepository&gt;().To&lt;ConsumerRepository&gt;(); } </code></pre> <p>I get an error when </p> <pre><code>var kernel = new StandardKernel(); </code></pre> <p>is called</p> <blockquote> <p>Method 'GetFilters' in type 'Ninject.Web.WebApi.Filter.DefaultFilterProvider' from assembly 'Ninject.Web.WebApi, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' does not have an implementation.</p> </blockquote> <p>What am I missing?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload