繁体   English   中英

在asp.net web form中实现autofac

[英]Implement autofac in asp.net web form

我正在开发 ASP.net Web Form,我想在网站中实现 AutoFac。

我已按照以下链接中的步骤操作: https : //autofaccn.readthedocs.io/en/latest/integration/webforms.html

但我收到了这个错误:

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

我在我的项目中找不到Global.asax.cs ,只有Global.asax

Global.asax :

<%@ Application Language="C#" %>
<%@ Import Namespace="Autofac" %>
<%@ Import Namespace="Autofac.Integration.Web" %>
<script RunAt="server">
    public class Global : HttpApplication, IContainerProviderAccessor {

        // Provider that holds the application container.
        static IContainerProvider _containerProvider;

        // Instance property that will be used by Autofac HttpModules
        // to resolve and inject dependencies.
        public IContainerProvider ContainerProvider {
            get { return _containerProvider; }
        }

        void Application_Start(object sender, EventArgs e) {
            // Code that runs on application startup

            // Build up your application container and register your dependencies.
            var builder = new ContainerBuilder();
            //builder.RegisterType<SomeDependency>();
            // ... continue registering dependencies...

            // Once you're done registering things, set the container
            // provider up with your registrations.
            _containerProvider = new ContainerProvider(builder.Build());
        }
    }
</script>

有任何想法吗? 非常感谢!

我也遇到了这个问题,并通过创建 global.asax.cs 文件并从原始 global.asax 文件中引用它来修复它。 这使您能够实现所需的 IContainerProviderAccessor 接口。

文件:Global.asax

<%@ Application Codebehind="Global.asax.cs" Inherits="MyCompany.WebForms.Global" Language="C#" %>

文件:Global.asax.cs

using Autofac;
using Autofac.Core;
using Autofac.Integration.Web;
using ....


namespace MyCompany.WebForms
{ 
    public partial class Global : HttpApplication, IContainerProviderAccessor
    {
        // Provider that holds the application container.
        static IContainerProvider _containerProvider;

        // Instance property that will be used by Autofac HttpModules
        // to resolve and inject dependencies.
        public IContainerProvider ContainerProvider
        {
            get { return _containerProvider; }
        }

        protected void Application_Start(object sender, EventArgs e)
        {
             ....
        }
    }
}

实施此操作后,在将Global.asax.cs文件移动到App_Code文件夹并更新CodeBehind引用Global.asax之前,我也遇到了无法加载类型“MyCompany.WebForms.Global”错误的问题。

<configuration>
  <system.web>
<httpModules>
  <!-- This section is used for IIS6 -->
  <add
    name="ContainerDisposal"
    type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"/>
  <add
    name="PropertyInjection"
    type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web"/>
</httpModules>
</system.web>
    <system.webServer>
    <!-- This section is used for IIS7 -->
<modules>
  <add
    name="ContainerDisposal"
    type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"
    preCondition="managedHandler"/>
  <add
    name="PropertyInjection"
    type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web"
    preCondition="managedHandler"/>
</modules>
</system.webServer>
</configuration>

这些部分必须在 web.config 文件中定义,以便注入工作。 请参阅https://code.google.com/p/autofac/wiki/WebFormsIntegration#Implement_IContainerProviderAccessor_in_Global.asax

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM