Ich erstelle ein neues ASP.NET Web API-Projekt. Ich benutze dann nugget, um Ninject.Web.Common zu ziehen, dann lade ich Ninject.Web.WebApi von here herunter und baue es. Beinhaltet es im Projekt. Ich habe einen Service und die Einspritzung über Konstruktor Setup-Bindung (der Debugger zeigt, dass der Code tatsächlich die bind Hits), aber immer noch wirft dieser Fehler:NinjectWebCommon Bindings in RegisterServices funktioniert nicht für mich in WebApi
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 meinem Projekt:
- Ninject. dll
- Ninject.Web.Common
- Ninject.Web.Webapi
- WebActivator
Alle ninject Bibliotheken als Version 3.
Hier markiert sind, 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;
}
}
Die heruntergeladene Projekt Probe von GitHub funktioniert, aber nicht wenn ich ein neues Projekt erstelle. Vermisse ich einen Schritt?
Dank schaffen, ich Ich wusste nicht, dass es ein kleines Paket gab. Hat die Installation des Pakets etwas anderes zum Projekt gemacht, was ich nicht tun würde, wenn ich die DLL manuell einfüge? –
Ich habe keine Ahnung, was du getan hast. Das Paket macht nichts Besonderes. –
Wie könnte das "Ihr Code sieht gut aus. Es muss ein Problem woanders sein." die akzeptierte Antwort sein? –