0

iam using ninject.web extension in my web application but now i have a problem reolving a dependence in Session_Start method of global.asax

this is my global.asax

public class Global : NinjectHttpApplication
    {
        [Inject]
        IUserManagement um { get; set; }

        protected void Session_Start(object sender, EventArgs e)
        {

            if (WebUser.user != null)
            {               
            if (HttpContext.Current.Session[ChiaveSessioneUtente] == null)
            {
                if (HttpContext.Current != null)
                    HttpContext.Current.Session.Add(ChiaveSessioneUtente, um.ResolveRequestingUser(Request));                   
            }     
        }
        protected override IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            RegisterServices(kernel);
            return kernel;
        }   
        private void RegisterServices(IKernel kernel)
        {
            var modules = new List<INinjectModule>
            {
                new BusinessLogicModule(),
            };
            kernel.Load(modules);
        }
}

Ninject module

public class BusinessLogicModule : NinjectModule
{
    public override void Load()
    {
        Bind<IBusinessInquiry>().To<BusinessInquiry>();     
        Bind<IUserManagement>().To<UserManagement>();
    }   
}

but when i start application um is null, even if via debug i see that CreateKernel is executed before Session_Start

i also try the nuget package version of ninject.web with NinjectWebCommon but the result is the same: property is not injected and is null

instead in my webForm all property injected works without problem, so problem is only with global.asax or session_Start method

gt.guybrush
  • 1,320
  • 3
  • 19
  • 48
  • 1
    I am afraid you will have to add code to get your kernel and call Resolve method inside Session_Start method – Red Sep 07 '16 at 09:47
  • so no way to avoid service locator? i just try now a service locator way and it's works fine but not so clean... – gt.guybrush Sep 07 '16 at 10:02
  • 1
    I am not really familiar with Ninject implementation, but I'm afraid injection is not applicable to some of the objects related to asp.net, and HttpApplication is in that list, because that's an entry point that allows for injection in all dependants, but not in itself. You might have to check sources for Ninject to prove that though. – Red Sep 07 '16 at 10:35
  • I may be wrong but I am pretty sure ninject automatically injects itself after the kernel is set up if you use the web module. – Marie Jan 15 '20 at 17:25

2 Answers2

-1

Property Setter Injection

Here’s an example of the same Samurai class using Property Setter Injection. For a property to be injected it must be annotated with [Inject].

class Samurai 
{    
    [Inject]
    public IWeapon Weapon { private get; set; }

    public void Attack(string target) 
    {
        this.Weapon.Hit(target);
    }
}
-3

try to remove [Inject] attribute in Global.asax

  • I think if he were to acheive this with property injection, he will need to mark the property as public as well? (I think Ninject's default configuration is not to bind non public properties when doing property injection) – adaam Sep 07 '16 at 10:05
  • Adaam agry with you: – Danilov Daniil Sep 07 '16 at 10:19