NinjectWebCommon Bindungen in RegisterServices nicht für mich arbeiten, in der WebApi

Erstelle ich einen neuen ASP.NET Web-API-Projekt. Ich habe dann nuget ziehen Ninject.Web.Common, dann habe ich den download und build-Ninject.Web.WebApi von hier. Eingeschlossen in das Projekt. Ich fügte hinzu, eine service-und die injection per Konstruktor, setup-Bindung (der debugger zeigt, dass der code tatsächlich trifft das binden), aber trotzdem wirft diese Fehlermeldung:

Error activating IValueService
No matching bindings are available, and the type is not self-bindable.
Activation path:
  2) Injection of dependency IValueService into parameter valueService of constructor of type ValuesController
  1) Request for ValuesController

Suggestions:
  1) Ensure that you have defined a binding for IValueService.
  2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
  3) Ensure you have not accidentally created more than one kernel.
  4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
  5) If you are using automatic module loading, ensure the search path and filters are correct.

Bibliotheken in mein Projekt:

  1. Ninject.dll
  2. Ninject.Web.Gemeinsame
  3. Ninject.Web.Webapi
  4. WebActivator

Alle ninject-Bibliotheken sind gekennzeichnet als version 3.

Hier ist der Code:

[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication3.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication3.App_Start.NinjectWebCommon), "Stop")]
namespace MvcApplication3.App_Start
{
using System;
using System.Web;

using Microsoft.Web.Infrastructure.DynamicModuleHelper;

using Ninject;
using Ninject.Web.Common;

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    ///<summary>
    ///Starts the application
    ///</summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    ///<summary>
    ///Stops the application.
    ///</summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    ///<summary>
    ///Creates the kernel that will manage your application.
    ///</summary>
    ///<returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);
        return kernel;
    }

    ///<summary>
    ///Load your modules or register your services here!
    ///</summary>
    ///<param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {

        kernel.Bind<IValueService>().To<ValueService>();
    }        
}

}

service:

public interface IValueService
{
    List<string> GetStrings();
}

public class ValueService : IValueService
{
    public List<string> GetStrings()
    {
        return new List<string>{"test", "test"};
    }
}

controller:

public class ValuesController : ApiController
{
    private readonly IValueService valueService;

    //GET /api/values
    public ValuesController(IValueService valueService)
    {
        this.valueService = valueService;
    }
}

Dem heruntergeladenen Projekt-sample von github funktioniert aber nicht, wenn ich ein neues Projekt erstellen. Bin ich fehlt ein Schritt?

InformationsquelleAutor Shawn Mclean | 2012-04-01
Schreibe einen Kommentar