[英]Implement autofac in asp.net web form
I am developing ASP.net Web Form, and I want to implement AutoFac into the website.我正在开发 ASP.net Web Form,我想在网站中实现 AutoFac。
I have followed the steps in following link: https://autofaccn.readthedocs.io/en/latest/integration/webforms.html我已按照以下链接中的步骤操作: https : //autofaccn.readthedocs.io/en/latest/integration/webforms.html
But I got this error:但我收到了这个错误:
This module requires that the HttpApplication (Global Application Class) implements IContainerProviderAccessor.
此模块要求 HttpApplication(全局应用程序类)实现 IContainerProviderAccessor。
I can't find Global.asax.cs
in my project, only Global.asax
.我在我的项目中找不到
Global.asax.cs
,只有Global.asax
。
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>
Any ideas?有任何想法吗? Thank you very much!
非常感谢!
I had this problem too and fixed it by creating a global.asax.cs file and referencing this from the original global.asax file.我也遇到了这个问题,并通过创建 global.asax.cs 文件并从原始 global.asax 文件中引用它来修复它。 This enables you to implement the required IContainerProviderAccessor interface.
这使您能够实现所需的 IContainerProviderAccessor 接口。
File: Global.asax文件:Global.asax
<%@ Application Codebehind="Global.asax.cs" Inherits="MyCompany.WebForms.Global" Language="C#" %>
File: Global.asax.cs文件: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)
{
....
}
}
}
After implementing this, I also had problems with Could not load type 'MyCompany.WebForms.Global' errors until I moved Global.asax.cs file into the App_Code folder and updated the CodeBehind reference Global.asax .实施此操作后,在将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>
These sections must be defined in the web.config file in order for injection to work.这些部分必须在 web.config 文件中定义,以便注入工作。 See https://code.google.com/p/autofac/wiki/WebFormsIntegration#Implement_IContainerProviderAccessor_in_Global.asax
请参阅https://code.google.com/p/autofac/wiki/WebFormsIntegration#Implement_IContainerProviderAccessor_in_Global.asax
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.