How to use DI container when OwinStartup

后端 未结 3 1076
谎友^
谎友^ 2020-12-02 13:56

It\'s a Web API 2 project.

When I implement DI using Ninject, I got an error message

An error occurred when trying to create a controller of type \'TokenCont

3条回答
  •  悲哀的现实
    2020-12-02 14:49

    We use the standard ninject.MVC5 package installed with nuget

    PM> install-package ninject.MVC5

    Then we configure our bindings like so.

    kernel.Bind()
        .To()
        .InRequestScope();
    
    kernel.Bind>()
        .To>()
        .InRequestScope();
    
    kernel.Bind()
        .To()
        .InRequestScope()
        .WithConstructorArgument("ApplicationName");
    
    kernel.Bind().ToSelf().InRequestScope()
        .WithPropertyValue("UserTokenProvider",
            new DataProtectorTokenProvider(
                kernel.Get().Create("EmailConfirmation")
                ));
    

    You may need to adjust dependent on how much you have customized your user model. For instance the user store binding may be something like.

    kernel.Bind>()
          .To().InRequestScope();
    

    Also any setting up of the user manger you require i.e. password policies can be set in your user manager constructor.

    Previously this could be found in the create method in the sample, you will no longer require this. also you can now get rid of the owin get context calls as ninject will handle resolution for you.

提交回复
热议问题