[英]How to use Ninject bootstrapper in WebApi OwinHost Startup?
I am migrating from IIS WebAPI to OwinHost. 我正在从IIS WebAPI迁移到OwinHost。 Utilizing the latest pre-release versions of nuget packages, I successfully used instructions here:
利用nuget软件包的最新预发布版本,我在这里成功使用了说明:
https://github.com/ninject/Ninject.Web.Common/wiki/Setting-up-a-OWIN-WebApi-application https://github.com/ninject/Ninject.Web.Common/wiki/Setting-up-a-OWIN-WebApi-application
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)
{
...
}
But in my code and the documentation example, the Ninject kernel is not created until after Startup. 但是在我的代码和文档示例中,Ninject内核直到启动后才会创建。 I need Ninject DI, however, in the Startup registration process for Cors and OAuth middleware registration.
但是,在Cors和OAuth中间件注册的启动注册过程中,我需要Ninject DI。 Before migrating to OwinHost, I could do something like this:
在迁移到OwinHost之前,我可以这样做:
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);
}
But internally, OwinBootstrapper.Execute will end up calling CreateKernel and bootstrapper.Initialize a second time, with bad consequences. 但在内部,OwinBootstrapper.Execute将最终调用CreateKernel和bootstrapper.Initialize第二次,结果不好。
What is the right way to create and use the ninject kernel within Startup and still register the Ninject/WebAPI middleware? 在Startup中创建和使用ninject内核并仍然注册Ninject / WebAPI中间件的正确方法是什么?
Add the following nuget-packages to your application: 将以下nuget-packages添加到您的应用程序:
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. 如果您使用的是web api版本5.0.0.0,则还需要从repo下载Ninject Resolver类以避免兼容性问题。
Create a static method that returns a Kernel object 创建一个返回Kernel对象的静态方法
public static class NinjectConfig
{
public static IKernel CreateKernel()
{
var kernel = new StandardKernel();
//Create the bindings
kernel.Bind<IProductsRepository>().To<ProductRepository>();
return kernel;
}
}
Then you can use ninject in your startup class 然后你可以在你的启动类中使用ninject
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);
}
}
Create the kernel
manually and then make UseNinjectMiddleware
use the same one instead of creating another. 手动创建
kernel
,然后使UseNinjectMiddleware
使用相同的kernel
,而不是创建另一个。
public void Configuration(IAppBuilder app)
{
var kernel = CreateKernel()
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
// USE kernel here
app.UseNinjectMiddleware(() => kernel);
app.UseNinjectWebApi(config);
}
In addition to Olif's post: 除了Olif的帖子:
install-package ninject.extensions.conventions install-package ninject.extensions.conventions
In your startup class add: using Ninject.Extensions.Conventions; 在你的启动类中添加:using Ninject.Extensions.Conventions;
Instead of manually binding: 而不是手动绑定:
public static IKernel CreateKernel() { var kernel = new StandardKernel(); //Create the bindings kernel.Bind<IProductsRepository>().To ProductRepository (); return kernel; }
You can now do: 你现在可以这样做:
kernel.Bind(x =>
{
x.FromThisAssembly()
.SelectAllClasses()
.BindDefaultInterface();
});
This will look through your assembly, and do the binding automatically. 这将查看您的程序集,并自动执行绑定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.