ASP.NET MVC + Ninject: InRequestScope

后端 未结 1 874
孤城傲影
孤城傲影 2021-01-27 13:47

I want to create instance of PerRequestResourceProvider using ninject InRequestScope:

public class PerRequestResourceProvider: IPerRequestResourceProvider
{
             


        
相关标签:
1条回答
  • 2021-01-27 14:49

    There are two issues with your code:

    1. Ninject is not getting initialized correctly. You need one of the Ninject.MVCx packages (according to the MVC version you are using). To configure it correctly, see: http://github.com/ninject/ninject.web.mvc

    2. You are injecting PerRequestResourceProvider (the class type), and not IPerRequestResourceProvider (the interface type) into HomeController, thus the .InRequestScope() defined on the IPerRequestResourceProvider binding is not taking any effect. Change the HomeController constructor to get the inteface type injected and you're good.


    Ninject does not require bindings for instantiatable (non-abstract,..) classes. This is why it is not obvious when the wrong binding is used.

    0 讨论(0)
提交回复
热议问题