This module requires HttpApplication to implement IContainerProviderAccessor

Hi, I want to use Autofac in my asp.net mpc application, and here is the code I have in the global.asxc file:

 protected void Application_Start()
    {
        ....

        var builder = new ContainerBuilder();
        builder.RegisterControllers(Assembly.GetExecutingAssembly());

        IContainer container = builder.Build();

        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

    }

but when I run the project, I see this error:

This module requires HttpApplication (Global Application Class) to implement IContainerProviderAccessor

what's wrong?

+3
source share
2 answers

The minimal global.asax.cs installation for autofac for asp.net mvc3 might look like this: (RegisterRoutes is removed from the code). Unlike previous versions of asp.net mvc (from http://code.google.com/p/autofac/wiki/Mvc3Integration )

HttpApplication IContainerProviderAccessor, ASP.NET. , , Global.asax.cs.

Autofac.Integration.Mvc.dll

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;

namespace ApplicationX
{
    public class MvcApplication : HttpApplication
    {
        private static IContainer _container;

        /// <summary>
        /// Gets the container.
        /// </summary>
        public IContainer Container
        {
            get { return _container; }
        }

        // RegisterRoutes and RegisterGlobalFilters removed ...

        /// <summary>
        /// Fired when the first resource is requested from the web server and the web application starts
        /// </summary>
        protected void Application_Start()
        {
            // Register: create and configure the container
            _container = BootstrapContainer();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));

            // MVC Stuff
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        /// <summary>
        /// Fired when the web application ends
        /// </summary>
        public void Application_End()
        {
            // Release: remember to dispose of your container when your application is about to shutdown to let it gracefully release all components and clean up after them
            _container.Dispose();
        }

        /// <summary>
        /// Bootstrapper is the place where you create and configure your container
        /// </summary>
        /// <returns>An Autofac container</returns>
        private IContainer BootstrapContainer()
        {
            var builder = new ContainerBuilder();
            // You can make property injection available to your MVC views by adding the ViewRegistrationSource to your ContainerBuilder before building the application container.
            builder.RegisterSource(new ViewRegistrationSource());
            // An example of a module that registers the dependencies for a ServiceLayer of your application
            builder.RegisterModule(new ServiceModule());
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            return builder.Build();
        }
    }
}
+2

, OP, .

:

  • IContainerProviderAccessor Global.asax. ASP.NET MVC.
  • AutofacControllerFactory. MVC MVC DependencyResolver.
  • HTTP- ASP.NET Autofac. Autofac ContainerDisposal PropertyInjection web.config. .
+5

All Articles