How to use Ninject bootstrapper in WebApi OwinHost Startup?

前端 未结 3 715
借酒劲吻你
借酒劲吻你 2021-02-01 20:52

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/ninje

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 21:18

    In addition to Olif's post:

    1. install-package ninject.extensions.conventions

    2. In your startup class add: using Ninject.Extensions.Conventions;

    3. Instead of manually binding:

     public static IKernel CreateKernel()
         {
             var kernel = new StandardKernel();
             //Create the bindings
             kernel.Bind().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.

提交回复
热议问题