simple injector in Web api project IIS 7

Jun 27, 2013 at 6:57 AM
hi
I'm trying to use simple injector in a Web api project on a website using Integrated mode under IIS7.

I get an exception Request is not available in this context.

[HttpException]: Request is not available in this context
at System.Web.HttpContext.get_Request()

For what i read this is because the HttpContext is not available on the Application_Start()
where i'm configuring the container.

Is there a better place to configure the container?
I[m using WebRequestLifestyle

Thank you very much in advanced.
Coordinator
Jun 27, 2013 at 8:01 AM
Can you supply a bit more information about your situation? What are you trying to resolve during startup and why?
Jun 27, 2013 at 8:05 AM
hi this is the code i have on my Application_Start

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {


        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        log4net.Config.XmlConfigurator.Configure();

        // configure the injection dependencies
        Container container = new Container();

        var services = GlobalConfiguration.Configuration.Services;
        var controllerTypes = services.GetHttpControllerTypeResolver().GetControllerTypes(services.GetAssembliesResolver());

        var webLifestyle = new WebRequestLifestyle();
        // register dao
        container.Register<IUserDao, EntityCacheUserDao>(Lifestyle.Transient);
        container.Register<IStudentDao, EntityStudentDao>(Lifestyle.Transient);
        container.Register<ILogDao, EntityLogDao>(Lifestyle.Transient);
        container.Register<IStudentDaoNew, EntityStudentDaoNew>(Lifestyle.Transient);

        // register managers
        container.Register<IUserService, UserServiceManager>(Lifestyle.Transient);
        container.Register<IStudentService, StudentServiceManagerNew>(Lifestyle.Transient);

        // register Web api contollers
        foreach (var controllerType in controllerTypes)
        {
            container.Register(controllerType);

        }

        // Register the dependency resolver.
        GlobalConfiguration.Configuration.DependencyResolver =
            new InjectionConfiguration(container);


        // Verify the container configuration
        container.Verify();


    } // end application start
I'm trying to set up the different dependencies for the Interfaces to provide with the implementation objects of those interfaces. I took the example from the integration guide but it fails to execute on IIS7.
Coordinator
Jun 27, 2013 at 9:09 AM
And at which line does your registration fail?
Jun 27, 2013 at 9:45 AM
when i execute

// Verify the container configuration
    container.Verify();
if throws the exception i described. The error is documented here http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart/

my question would be whether there is a better place to put the injection code rather than on the Application_Start so that i don't get the exception
Coordinator
Jun 27, 2013 at 10:03 AM
That's odd. The WebRequestLifestyle is designed to work without a HttpContext.Current in case it is running during verification. Can you post the stack trace (including stack traces of all inner exceptions)?
Coordinator
Jun 27, 2013 at 10:41 AM
The Application_Start is the right place to place your DI configuration and in most cases the right place to verify your configuration. Verifying your configuration however, means that the Container is instantiating (at least one) instance of every registered service to check if it can be created correctly. This means that any logic that is placed in a constructor will run at that point. If such constructor itself is directly or indirectly dependant on an HttpContext or one of its properties, that operation will fail because during application startup there is obviously no http context. In that case you are doing too much in your constructors.

Constructors should do as little as possible. Preferably only store its dependencies in private fields. Any other initialization can best be moved to a later point in time: after the object graph is constructed. This usually means at the moment that such instance is used for the first time by the application.
Coordinator
Mar 2, 2014 at 5:47 PM
Simple Injector v2.5 has been released and there is now an Web API integration package. This should fix most of your problems with integrating Simple Injector with ASP.NET Web API.

Good luck.
Marked as answer by dot_NET_Junkie on 3/2/2014 at 10:47 AM