0

I want to create instance of PerRequestResourceProvider using ninject InRequestScope:

public class PerRequestResourceProvider: IPerRequestResourceProvider
{
    priavte readonly _perRequestResorceInstance;
    public PerRequestResourceProvider()
    {
        _perRequestResorceInstance = new PerRequestResource();
    }
    public PerRequestResource GetResource()
    {
        return _perRequestResorceInstance;
    }
}

public interface IPerRequestResourceProvider
{
     PerRequestResource GetResource();
}

In my NinjectDependencyResolver:

.....
kernel.Bind<IPerRequestResourceProvider>.To<PerRequestResourceProvider>().InRequestScope();

I inject IPerRequestResourceProvider in few classes. But when I add breakpoint to PerRequestResourceProvider constructor I see that PerRequestResourceProvider is created three times during one request and not single per request. What's wrong?

Update: source code ttps://bitbucket.org/maximtkachenko/ninjectinrequestscope/src

mtkachenko
  • 5,389
  • 9
  • 38
  • 68
  • if that's the case you should post a minimal - but complete - sample which reproduces the problem. So that we/you can either create a bug report @ ninject or help you solve the problem. You could create a minimal solution, upload and link it. – BatteryBackupUnit Jul 24 '14 at 07:58
  • @BatteryBackupUnit I've added source code https://bitbucket.org/maximtkachenko/ninjectinrequestscope/src – mtkachenko Jul 24 '14 at 08:43
  • @BatteryBackupUnit I've fixed HomeController contsructor but result is the same: PerRequestResourceProvider constructor is called 3 times during request. – mtkachenko Jul 24 '14 at 10:44
  • 1
    yes i observed that too. As far as i've found out yet, i would say that you did not integrated ninject correctly into the solution. It's too bad that ninject is not throwing an exception. I guess you need ninject.MVC3 or ninject.extensions.MVC5 or something like that. The ninject.web.common stuff is IMHO not getting initialized properly. – BatteryBackupUnit Jul 24 '14 at 10:52
  • see https://github.com/ninject/ninject.web.mvc on how to do that – BatteryBackupUnit Jul 24 '14 at 10:56
  • 1
    @BatteryBackupUnit I've installed Ninject.MVC4 and now it works properly. Thanks. – mtkachenko Jul 24 '14 at 10:59

1 Answers1

0

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.

BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68