Inject dependency into Global.asax in ASP MVC app

Jun 27, 2013 at 3:37 PM
Is there a way to configure Simple Injector to inject a dependency into the Global.asax.cs? This is an ASP MVC app and I have a dependency that needs to be injected so I can use it in the Application_BeginRequest and Application_AuthenticateRequest methods.

Jun 27, 2013 at 4:42 PM
I was able to resolve my dependency by using the following in my Global.asax file:
Does this seem to be an appropriate way of doing this? Is there a better way?

Jun 27, 2013 at 5:17 PM
You can use the DependencyResolver.Current.GetService<T>() method, but there is a down side to this. When the type you request is not registered or could not be resolved correctly, the method will return null instead of throwing an exception.

So instead of indirectly querying the container through the DependencyResolver, you can store a reference to the container in the Global.asax at startup. This way you can directly call into the GetInstance<T> method of the container. This method will never return null, but will throw when there is a problem.
public static void Initialize()
    // normal container configuration here.

    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));


public class MvcApplication : System.Web.HttpApplication
    private static SimpleInjector.Container container;
    public static void SetContainer(Container c) { container = c; }

    protected void Application_BeginRequest()
        container.GetInstance<ILogger>().Log("Request started");
Marked as answer by dot_NET_Junkie on 3/2/2014 at 10:47 AM