如何使用Ninject引导程序中的WebAPI

如何使用Ninject引导程序中的WebAPI

本文介绍了如何使用Ninject引导程序中的WebAPI OwinHost启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从IIS的WebAPI迁移到OwinHost。利用的NuGet包的最新pre-发布版本,我成功地使用说明操作:

I am migrating from IIS WebAPI to OwinHost. Utilizing the latest pre-release versions of nuget packages, I successfully used instructions here:

https://github.com/ninject/Ninject.Web.Common/wiki/Setting-up-a-OWIN-WebApi-application

下面是我的code的存根:

Here is stub of my code:

    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();
        config.MapHttpAttributeRoutes();

        app.UseNinjectMiddleware(CreateKernel);
        app.UseNinjectWebApi(config);
    }

    private static StandardKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());
        RegisterServices(kernel);
        return kernel;
    }

    private static void RegisterServices(IKernel kernel)
    {
        ...
    }

但在我的code和文件的例子,直到启动后未创建Ninject内核。我需要Ninject DI,然而,在用于一个Cors和OAuth中间件登记启动注册过程。迁移到OwinHost之前,我可以做这样的事情:

But in my code and the documentation example, the Ninject kernel is not created until after Startup. I need Ninject DI, however, in the Startup registration process for Cors and OAuth middleware registration. Before migrating to OwinHost, I could do something like this:

public void Configuration(IAppBuilder app)
{
  _bootstrapper = new Bootstrapper();
  _bootstrapper.Initialize(CreateKernel);

  var config = new HttpConfiguration();
  config.MapHttpAttributeRoutes();

  // USE _boostrapper.Kernel here

  app.UseNinjectMiddleware(CreateKernel);
  app.UseNinjectWebApi(config);
}

但在内部,OwinBootstrapper.Execute最终会调用CreateKernel和bootstrapper.Initialize第二次,用不好的后果。

But internally, OwinBootstrapper.Execute will end up calling CreateKernel and bootstrapper.Initialize a second time, with bad consequences.

什么是创建和启动内使用ninject内核,仍然注册Ninject /中间件的WebAPI正确的方式?

What is the right way to create and use the ninject kernel within Startup and still register the Ninject/WebAPI middleware?

推荐答案

以下的NuGet的程序包添加到应用程序:

Add the following nuget-packages to your application:


  1. 安装封装Microsoft.AspNet.WebApi.Owin

  2. 安装封装Ninject

如果您正在使用Web API版本5.0.0.0你还需要下载Ninject从回购解析器类以避免兼容性问题。

If you are using web api version 5.0.0.0 you also need to download the Ninject Resolver class from the repo to avoid compatability issues.

创建一个返回内核对象的静态方法

Create a static method that returns a Kernel object

public static class NinjectConfig
{
    public static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        //Create the bindings
        kernel.Bind<IProductsRepository>().To<ProductRepository>();
        return kernel;
    }
}

然后你就可以在你的启动类使用ninject

Then you can use ninject in your startup class

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();
        config.DependencyResolver = new NinjectResolver(NinjectConfig.CreateKernel());

        config.Routes.MapHttpRoute("default", "api/{controller}/{id}", new { id=RouteParameter.Optional });

        app.UseWebApi(config);
    }
}

这篇关于如何使用Ninject引导程序中的WebAPI OwinHost启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:11