gpt4 book ai didi

c# - ASP.NET Web API 与 ninject 绑定(bind)

转载 作者:IT王子 更新时间:2023-10-29 04:02:10 25 4
gpt4 key购买 nike

我刚刚安装了 mvc4 rc 更新,我正在尝试构建一个 api 应用程序,但运气不佳。

我正在使用 ninject 但无法加载我的 Controller 。我不断收到错误

Type 'Api.Controllers.ConsumerController' does not have a default constructor

我对 mvc 和使用注入(inject)非常陌生,所以请多多包涵。

我没有对通过 nuget 创建的默认绑定(bind)做任何特殊的事情

 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<IConsumerRepository>().To<ConsumerRepository>();
}
}

我的 Controller 看起来像

   private readonly IConsumerRepository _repository;

public ConsumerController(IConsumerRepository repository)
{
_repository = repository;
}

[HttpGet]
public IQueryable<Consumer> Get(Guid id)
{
return _repository.Get(id).AsQueryable();
}

我需要做什么才能让 api Controller 与 ninject 一起工作?

对不起,如果这是简单的事情

我尝试了迈克尔的建议,但是在将 webcommon.cs 更改为这个之后

  private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(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<IConsumerRepository>().To<ConsumerRepository>();
}

我得到一个错误
var kernel = new StandardKernel();

被称为

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.

我错过了什么?

最佳答案

我问了Brad Wilson关于这一点,它在 MVC4 RC 中发生了变化。

GlobalConfiguration.Configuration.ServiceResolver 已移至 GlobalConfiguration.Configuration.DependencyResolver

使用此实现为您的 Web Api 创建一个 Ninject DependencyResolver: https://gist.github.com/2417226

NinjectWebCommon.cs中:

// Register Dependencies
RegisterServices(kernel);

// Set Web API Resolver
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

关于c# - ASP.NET Web API 与 ninject 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10849132/

25 4 0