2014-01-20 3 views
2

ASP.net 웹 양식을 개발 중이며 웹 사이트에 AutoFac을 구현하고 싶습니다.asp.net 웹 양식에서 autofac 구현

나는 다음 링크의 단계를 따랐다 : https://code.google.com/p/autofac/wiki/WebFormsIntegration

하지만이 오류가 발생했습니다 : 내 프로젝트 Global.asax.csGlobal.asax을 찾을 수 없습니다

This module requires that the HttpApplication (Global Application Class) implements IContainerProviderAccessor.

.

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 파일이 폴더에 있으며, 폴더가 있고 코드가 업데이트되었습니다. 참조 Global.asax.

관련 문제