2014-09-11 4 views
0

PreApplicationStart 메서드를 사용하여 HttpModule을 등록하고 IoC 컨테이너를 초기화하는 StructureMap.MVC5를 사용하고 있습니다. IoC 초기화에서 환경 특정 속성을 설정할 수 있도록이 시점에서 페이지가 실행되는 서버 이름을 가져올 수 있습니까?PreApplicationStart의 현재 URL

+0

이 문제를 해결 한 행운을 빕니다. – Saravanan

+0

@ saravanan, 예,이 문제를 해결할 수있었습니다. 방금 내 해결책을 게시했습니다. 희망이 도움이됩니다! –

답변

0

HttpContext이 이미 설정된 Application_BeginRequest 방법으로 IoC 초기화를 이동하여이 문제를 해결할 수있었습니다. Application_BeginRequest에 대한 각 호출시 IoC 컨테이너가 다시 초기화되지 않도록하기 위해 뮤텍스 블록을 사용할 수 있었으므로이 코드를 페이지 수명주기의 이전 단계로 옮길 필요가 없습니다.

public class FirstInitialization 
{ 
    private static Object s_lock = new Object(); 

    public static string URL { get; protected set; } 

    // Initialise only on the first request 
    public static string Initialize(HttpContext context) 
    { 
     if (string.IsNullOrEmpty(URL)) 
     { 
      lock (s_lock) 
      { 
       if (string.IsNullOrEmpty(URL)) 
       { 
        URL = HttpContext.Current.Request.Url.AbsoluteUri; 
       } 

       DependencyResolver.SetResolver(IoC.GetDependencyResolver(URL)); 

      } 
     } 

     return URL; 
    } 
}