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 6677303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:06:07+00:00 2026-05-26T04:06:07+00:00

How do I inject dependencies into the global.asax.cs, i.e. the MvcApplication class? Having previously

  • 0

How do I inject dependencies into the global.asax.cs, i.e. the MvcApplication class?

Having previously used the Service Locator (anti-)pattern for dependency injection, I am trying to follow best practice advice in my latest MVC application by using an IOC container (specifically Unity.Mvc3 because it comes with an implementation of the IDependencyResolver out of the box) and constructor injection.

Everything seems quite straight forward so far except for a couple of snags, one of which is in the global.asax.cs (the other is for custom attributes but there’s aleady a question on SO covering that).

The HttpApplication event handlers in the MvcApplication class such as:

Application_Start()
Application_EndRequest(object sender, EventArgs e)
Application_AcquireRequestState(object sender, EventArgs e)

may require external dependencies, e.g. a dependency on an ILogService. So how do I inject them without resorting to the service locator (anti-)pattern of e.g.

private static ILogService LogService
{
    get
    {
        return DependencyResolver.Current.GetService<ILogService>();
    }
}

Any help/advice greatly appreciated!

  • 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-05-26T04:06:08+00:00Added an answer on May 26, 2026 at 4:06 am

    The class in your global.asax.cs is your Composition Root, so you can’t (and shouldn’t) inject anything into it from the outside.

    However, there’s only one instance of the MvcApplication class, so if you need a service in one of its methods, you can just declare it as a member field – e.g:

    public class MvcApplication : System.Web.HttpApplication
    {
        private readonly ILogService log;
    
        public MvcApplication()
        {
            this.log = new MyLogService();
        }
    
        protected void Application_Start()
        {
            // ...
    
            this.log.Log("Application started");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you're using a factory pattern, how do you inject dependencies into constructors at
I'm using Spring 3 and want to inject some dependencies into a class that
Does anybody know of a way to use Guice to inject dependencies into the
I'm looking for suggestions on how to inject runtime dependencies into JPA entities retrieved
Is it possible to inject an interface into an existing 3rd party class that
I have a WPF UserControl that I'd like to inject dependencies into. What's the
i'm trying to inject stuff into a custom ViewPage (ModelViewPage, from MvcContrib) public class
Question: Is there a way to use Ninject to inject dependencies into my MVC
Some dependency injection containers enable you to inject configured services into an already constructed
I'm having trouble using dependency injection (with CDI/Weld) within libraries used in a war

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.