Ninject Create an additional instance using NinjectHttpModule

For some reason, Ninject creates an extra instance of my object when I use NinjectHttpModuleMVC 4 in my application.

If I use NinjectHttpModule(by default Ninject.MVC3), but actually doesn't have classes IHttpModulerequiring constructor injection, it works fine. But as soon as I create a class that implements IHttpModuleand requires constructor injection, Ninject for some reason creates two instances of my object.

I added the tracking code to a class that is duplicated to make sure that it is duplicated. Each time an instance is created, the static count variable is incremented:

namespace Trigger.Events
{
    public class TriggerEventRegistry : ITriggerRegistry
    {
        private static int count;

        public TriggerEventRegistry()
        {
            TriggerEventRegistry.count++;
        }
    }
 }

Here is mine IHttpModule:

namespace TriggerDevelopment.ApplicationTriggers
{
    public class RegisterTriggerComponentsHttpModule : IHttpModule
    {
        ITriggerEventRegistry eventRegistry;

        public RegisterTriggerComponentsHttpModule(ITriggerEventRegistry eventRegistry)
        {
            this.eventRegistry = eventRegistry;
        }
     }
     ....
}

, a TriggerEventRegistry ( ), TriggerEventRegistry.count 2. RegisterTriggerComponentsHttpModule, TriggerEventRegistry.count 1 ( , /).

:

Bind<ITriggerEventRegistry>().To<TriggerEventRegistry>().InRequestScope();

!

, , HTTP-, , - . .

, ctor Init RegisterTriggerComponentsHttpModule .

+5
1

HttpModule , . , - , favicon. - , ...

0

All Articles