Injecting HttpContext in Ninject 2

前端 未结 1 877
礼貌的吻别
礼貌的吻别 2021-02-05 15:09

In my asp.net mvc application I\'m using Ninject as a DI framework.

My HttpAccountService is used by my controllers to get info from and to cookies. For this I need the

相关标签:
1条回答
  • 2021-02-05 15:52

    WithConstructorArgument has an overload that takes a Func<NinjectContext,T>, i.e., you can use:

    ... .WithConstructorArgument("context",ninjectContext=>HttpContext.Current);

    which will call the provided 'callback' lambda within the request processing and obtain the correct value at that point in time [as opposed to you calling the other overload and supplying a constant value which gets computed at Bind<> time].

    (If you're not trying to Mock the context, I assume you'll consider using it inline)

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