该模块要求 HttpApplication 实现 IContainerProviderAccessor

发布于 2024-11-07 14:27:10 字数 514 浏览 0 评论 0原文

你好 我想在我的 asp.net mvc 应用程序中使用 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

怎么了 ?

Hi
I want to use Autofac in my asp.net mvc appliation and here is the code I have in 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 that the HttpApplication (Global Application Class) implements IContainerProviderAccessor

what is wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深陷 2024-11-14 14:27:10

我和OP遇到了同样的问题,但我的解决方案不同。

来自此处

删除旧项目

  • 从 Global.asax 中删除 IContainerProviderAccessor 接口。ASP.NET MVC 集成不再使用此实现。
  • 删除对 AutofacControllerFactory 的引用。新的 MVC 集成改为使用 MVC DependencyResolver 类进行集成。
  • 删除 ASP.NET Autofac HTTP 模块配置。 之前需要一些 Autofac ContainerDisposal 和 PropertyInjection 模块在你的 web.config 中。这些应该被删除。

I had the same problem as the OP, but my solution was different.

From here:

Remove Old Items

  • Remove IContainerProviderAccessor interface from Global.asax. This implementation is no longer used by ASP.NET MVC integration.
  • Remove references to the AutofacControllerFactory. The new MVC integration uses the MVC DependencyResolver class for integration instead.
  • Remove ASP.NET Autofac HTTP module configurations. Previously there were some Autofac ContainerDisposal and PropertyInjection modules required in your web.config. These should be removed.
剪不断理还乱 2024-11-14 14:27:10

asp.net mvc3 的 autofac 的最小 global.asax.cs 设置可能如下所示:
(RegisterRoutes 已从代码中删除)。与以前版本的 asp.net mvc 不同(来自 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();
        }
    }
}

A minimal global.asax.cs setup for autofac for asp.net mvc3 could look like this:
(RegisterRoutes is removed from the code). As opposed to previous versions of asp.net mvc (from http://code.google.com/p/autofac/wiki/Mvc3Integration)

the HttpApplication class no longer needs to implement the IContainerProviderAccessor interface as described in the ASP.NET Integration documentation. All code related to implementing the interface should be removed your Global.asax.cs file.

You will also need a reference to 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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文