2014-01-20 102 views
2

我正在開發ASP.net Web窗體,並且我想要在網站中實現AutoFac。在asp.net web窗體中實現autofac

我遵循了以下鏈接步驟: https://code.google.com/p/autofac/wiki/WebFormsIntegration

但我得到這個錯誤:

This module requires that the HttpApplication (Global Application Class) implements 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> 

任何想法?非常感謝你!

回答

2
<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

+0

我已經在web.config中添加了,仍然有相同的錯誤... –

3

我也有這個問題,並通過創建一個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) 
     { 
      .... 
     } 
    } 
} 

實施在此之後,我還與問題未能加載類型「MyCompany.WebForms.Global '錯誤,直到我移動Global.asax.cs文件到App_Code文件夾和更新CodeBehind參考Global.asax