在OWIN Startup中使用NInject解决依赖关系

时间:2015-07-26 18:44:23

标签: c# asp.net-web-api ninject owin

我有以下代码:

public partial class Startup
{
    private IAccountService _account;

    public void Configuration(IAppBuilder app)
    {

        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        this._account.ConfigureAuth(app);
    }
}

我如何解决IAccountService的依赖?我知道首先执行NinjectWebCommon.cs并在此课程之后执行。

是否可以通过构造函数或配置方法中的参数解析此传递IAccountService?

1 个答案:

答案 0 :(得分:0)

你试过这样的绑定吗?

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
             RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

               this._account.ConfigureAuth(app);

            app.UseNinjectMiddleware(CreateKernel);
            app.UseNinjectWebApi(config);
        }
    }
    public static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();

        kernel.Bind<IAccountService>().To<AccountService>();
        return kernel;
    }

有一个使用OWIN + Unit的例子可以帮助你: https://damienbod.wordpress.com/2013/10/01/self-host-webapi-with-owin-and-unity/