该模块要求HttpApplication实现IContainerProviderAccessor

时间:2011-05-16 06:43:52

标签: asp.net-mvc autofac

您好 我想在我的asp.net mvc appliation中使用Autofac,这是我在global.asxc文件中的代码:

 protected void Application_Start()
    {
        ....

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

        IContainer container = builder.Build();

        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

    }

但是当我运行项目时,我看到了这个错误:

此模块要求HttpApplication(全局应用程序类)实现IContainerProviderAccessor

出了什么问题?

2 个答案:

答案 0 :(得分:7)

我遇到与OP相同的问题,但我的解决方案不同。

来自here

  

删除旧项目

     
      
  • 从Global.asax中删除IContainerProviderAccessor接口。 ASP.NET MVC集成不再使用此实现。
  •   
  • 删除对AutofacControllerFactory的引用。新的MVC集成使用MVC DependencyResolver类进行集成。
  •   
  • Remove ASP.NET Autofac HTTP module configurations.以前,您的web.config中需要一些Autofac ContainerDisposal和PropertyInjection模块。这些应该删除。
  •   

答案 1 :(得分:2)

asp.net mvc3的autofac的最小global.asax.cs设置可能如下所示: (RegisterRoutes已从代码中删除)。与以前版本的asp.net mvc(来自http://code.google.com/p/autofac/wiki/Mvc3Integration

相反
  

HttpApplication类不再需要实现ASP.NET集成文档中描述的IContainerProviderAccessor接口。应该删除与实现接口相关的所有代码的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();
        }
    }
}