2012-10-22 1 views
0

asp.net에서 autofac을 사용하고 있습니다. 그런 다음asp.net with autofac - webform의 모든 컨트롤이 null입니다.

AssertNotBuilt(); 
// Register Web Pages 
m_builder.RegisterAssemblyTypes(typeof(AboutPage).Assembly) 
    .Where(t => t.GetInterfaces().Contains(typeof(IHttpHandler))) 
    .AsSelf().InstancePerLifetimeScope(); 

m_container = m_builder.Build(); 
m_wasBuilt = true; 

나는 현재 웹 페이지 얻을 수있는 사용자 지정 HttpHandler를를 사용하십시오 :

public class ContextInitializerHttpHandler : IHttpHandler, IRequiresSessionState 
    { 
     public void ProcessRequest(HttpContext context) 
     { 
      //Get the name of the page requested 
      string aspxPage = context.Request.Url.AbsolutePath; 

      if (aspxPage.Contains(".aspx")) 
      { 
       // Get compiled type by path 
       Type webPageBaseType = BuildManager.GetCompiledType(aspxPage).BaseType; 

       // Resolve the current page 
       Page page = (Page)scope.Resolve(webPageBaseType); 

       //process request 
       page.ProcessRequest(context); 

      } 
     } 
     public bool IsReusable 
     { 
     get { return true; } 
     } 
    } 

모든 작품을 좋아,하지만 그때는 웹를 Page_Load에 들어갈 때의 Global.asax에 내 모든 웹 페이지를 등록 , 페이지에 존재하는 모든 ASP 컨트롤이 null 인 것을 볼 수 있습니다. 왜 그들은 null이고 어떻게 초기화합니까?

+0

당신은 여기에 몇 가지를 명확히해야 할 수도 있습니다

그래서 솔루션 (또는 해킹 ..) 웹 페이지를 등록하고 대신 범위에서 페이지 컨트롤을 해결하기 위해 아니었다. 아주 비표준적인 일을하는 것처럼 보입니다. Autofac 위키는 웹 양식 (https://code.google.com/p/autofac/wiki/AspNetIntegration)과의 통합 방법을 알려줍니다. Autofac이 제공하는 MODULE 통합이 아닌 HANDLER를 사용하는 것 같습니다. 사용자가 귀하의 페이지에 어떻게 액세스합니까? 모든 요청은 해당 처리기를 통해 파이프됩니까? 문제를 해결하기보다 페이지를 "새로 만들면"어떻게됩니까? 여전히 null입니까? –

답변

0

나는 그것을 알아 냈다. 내가 등록 페이지는 내 HTTP 처리기의 컨텍스트에서 수행 할 수있는 페이지처럼 컴파일되지 않습니다 :

string aspxPage = context.Request.Url.AbsolutePath; 
Type webPageBaseType = BuildManager.GetCompiledType(aspxPage); 

이들은 내가 모든 컨트롤을 보유 할 것을 필요로하는 페이지입니다. 문제는 동적 http 페이지 인 somewebpage_aspx 형태로 보이고 어셈블리가 App_Web_somewebpage.aspx.cdcab7d2.r3x-vs2n, 버전 = 0.0.0.0, Culture = neutral, PublicKeyToken이므로 http handler에서 등록 할 수 없다는 것입니다. = null.

ILifetimeScope scope = IocInitializer.Instance.InitializeCallLifetimeScope(); 
Type webPageType = BuildManager.GetCompiledType(aspxPage); 
Page page = (Page)Activator.CreateInstance(webPageType); 

foreach (var webPageProperty in webPageType.GetProperties(BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.Public)) 
{ 
    if (scope.IsRegistered(webPageProperty.PropertyType)) 
    { 
     var service = scope.Resolve(webPageProperty.PropertyType); 
     webPageProperty.SetValue(page, service, null); 
    } 
}