首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >本模块要求HttpApplication实现IContainerProviderAccessor

本模块要求HttpApplication实现IContainerProviderAccessor
EN

Stack Overflow用户
提问于 2011-05-16 14:43:52
回答 2查看 5.7K关注 0票数 4

嗨,我想在我的asp.net mvc应用程序中使用Autofac,这是我在global.asxc文件中的代码:

代码语言:javascript
复制
 protected void Application_Start()
    {
        ....

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

        IContainer container = builder.Build();

        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

    }

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

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

出什么问题了?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-16 16:28:56

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

HttpApplication类不再需要实现IContainerProviderAccessor接口,如ASP.NET集成文档中所述。所有与实现接口相关的代码都应该从Global.asax.cs文件中删除。

您还需要对Autofac.Integration.Mvc.dll的引用

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2013-11-21 01:30:31

我和操作员有同样的问题,但我的解决方案不同。

来自here

删除旧项目

来自

  • Remove的integration.
  • Remove接口。此实现不再用于ASP.NET MVC integration.
  • Remove对AutofacControllerFactory的引用。新的MVC集成使用MVC DependencyResolver类进行集成。以前,您的web.config中需要一些Autofac ContainerDisposal和PropertyInjection模块。这些都应该删除。
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6013976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档