Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8075375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:57:59+00:00 2026-06-05T14:57:59+00:00

this question Ninject Dependency Injection in MVC3 – Outside of a Controller is close

  • 0

this question Ninject Dependency Injection in MVC3 – Outside of a Controller is close to what I’m experiencing, but not quite.

I have an ASP.NET MVC3 site using Ninject 3 and it works wonderfully with constructor injection. All my dependencies are resolved, including those that pass in HttpContext.Current.

My issue is that in global.asax, I kick off a TaskManager class that periodically performs some tasks on a timer. Inside the TaskManager class, I don’t have controllers, so if I need access to one of my dependencies (like my error logging service), I use a static wrapper class that has access to the kernel object:

var logger = MyContainer.Get<ILoggingService>();
logger.Error("error doing something...", ex);

The .Get method simply performs a kernel.Get call resolve my dependency. Works great every time I use this method on my other dependencies. However, ILoggingService has a dependency called MyWebHelper that is injected via it’s constructor and includes HttpContext in it’s constructor.

    public class DefaultLogger : ILoggingService
    {     
        public DefaultLogger(IRepository<Log> logRepository, IWebHelper webHelper)
        {
            _logRepository = logRepository;
            _webHelper = webHelper;
       }
    }

    public class MyWebHelper : IWebHelper
    {
      public MyWebHelper(HttpContext httpContext)
      {
          _httpContext = httpContext;
      }
    }

In the rest of my web site, this all works just fine because all the dependencies are injected into my MVC controllers. But what doesn’t work is if I manually call my static wrapper class to get my dependencies that way. I get the error:

Error activating HttpContext using binding from HttpContext to method

Provider returned null.

So, it’s not giving me an HttpContext like it does throughout the rest of my MVC application. I hope this makes sense, I’m not a ninject expert yet, but I’m trying…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:58:00+00:00Added an answer on June 5, 2026 at 2:58 pm

    My issue is that in global.asax, I kick off a TaskManager class that
    periodically performs some tasks on a timer.

    That’s a bad idea as Phil Haack explains in details. Don’t do this in your web application. Those recurring tasks should be done in a separate application (Windows Service or some console application which is scheduled to run at regular intervals).

    Now the thing is that you are running background threads. Those background threads run outside of any user HTTP request and as a consequence HttpContext.Current is obviously null inside them. So even if you don’t follow Phil Haack’s advice and continue running background tasks in your ASP.NET application you will have to rearchitecture your method so that it no longer depends on any HttpContext because there’s no such thing in those background threads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be stupid question, but I am looking at Ninject sources and don't
This question might be naive as I'm new to ColdFusion programming. I have a
This question has been asked in a C++ context but I'm curious about Java.
This question might look naive, but I couldn't understand this code in the ViewModelLocator.cs
This question has been asked a lot of times in many forums. But I
I'm a beginner on IoC and dependency injection. I'm reading about it, but I
Following on from this question MVC3 - Should I design my Model to be
i have followed the pattern on this site to hook up ninject and nhibernate
I have a solution with two relevant (to this question) projects, and a few
I really don't know where to begin with this question, but the site I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.