2010-04-19 6 views
5

캐슬/윈저에 새로운 있습니다. 나와 함께하시기 바랍니다.성 PerRequestLifestyle을 인식 할 수 없음

나는 현재 프레임 워크 System.Web.Mvc.Extensibility를 사용하고 그 시작 코드, 그것은 다음과 같이 HttpContextBase 등록 : 내가하고 싶었던 무엇

container.Register(Component.For<HttpContextBase>().LifeStyle.Transient.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current))); 

가 될 httpContextBase의 라이프 스타일을 동작을 변경하고 변경하는 것입니다 PerWebRequest. 그러나,

System.Configuration.ConfigurationErrorsException: Looks like you forgot to 
register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule 
Add '<add name="PerRequestLifestyle" 
type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.MicroKernel" 
/>' to the <httpModules> section on your web.config 

내가 <system.web><system.webServer>에서 한 :

container.Register(Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current))); 

을 그러나, 나는이 작업을 수행 할 때, 나는 다음과 같은 오류가 발생했습니다 :

그래서 나는 다음에 코드를 변경해야 , 나는 여전히 같은 오류가 발생하고 있습니다. 어떤 힌트?

미리 감사드립니다.

업데이트

system.web.mvc.extensibility 프레임 워크에서

요청

당 코드 블록이이 같이 HttpApplication에서 상속 extendedMvcApplication라는 클래스이며, 위해 Application_Start 방법 추가, 그것은 호출 BootStrapper.Execute(). 당신이 볼 수 있듯이, 그것은 IBootStrapperTask의 목록을 반복하고이를 실행하려고

public void Execute() 
    { 
     bool shouldSkip = false; 

     foreach (IBootstrapperTask task in ServiceLocator.GetAllInstances<IBootstrapperTask>().OrderBy(task => task.Order)) 
     { 
      if (shouldSkip) 
      { 
       shouldSkip = false; 
       continue; 
      } 

      TaskContinuation continuation = task.Execute(ServiceLocator); 

      if (continuation == TaskContinuation.Break) 
      { 
       break; 
      } 

      shouldSkip = continuation == TaskContinuation.Skip; 
     } 
    } 

:이 방법이 구현은 다음과 같다. 내가 내 MVC 응용 프로그램의 경로를 등록 할 하나의 작업을 가지고 발생합니다

public class RegisterRoutes : RegisterRoutesBase 
{ 
    private HttpContextBase contextBase; 

    protected override TaskContinuation ExecuteCore(IServiceLocator serviceLocator) 
    { 
     contextBase = serviceLocator.GetInstance<HttpContextBase>(); 
     return base.ExecuteCore(serviceLocator); 
    } 

    protected override void Register(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
     routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); 
     routes.IgnoreRoute("{*robotstxt}", new { robotstxt = @"(.*/)?robots.txt(/.*)?" }); 

     XmlRouting.SetAppRoutes(routes, contextBase.Server.MapPath("~/Configuration/Routes.xml")); 
    } 
} 

당신은 내가의 getInstance (해결) 나는 XML 파일의 서버 경로를 얻을 수있는 것과 같은 httpcontextbase 객체에 필요한 것을 알 수 있습니다 .

+1

언제 해당 개체를 해결합니까? 어느 시점에서? Application_Start()의 –

+0

은 너무 일찍 끝났습니까? – Herman

+0

@Herman : 현재 지원되지 않습니다. Application_Start()에서 무엇을 해결하려고합니까? –

답변

7

이 글을 쓰는 PerWebRequest 라이프 스타일을 위해 Application_Start에서 해결을 지원하지 않습니다().

참조가 문제 설명 및 토론 :

해결 방법 : 인스턴스로

  1. 등록 RegisterRoutes이 명시 적으로 그것을 전달 삼 생성자 매개 변수로 레어 컨텍스트.:

    container.Register(Component.For<IBootstrapperTask>() 
              .Instance(new RegisterRoutes(Context))); 
    
  2. 사용 HostingEnvironment.MapPath 대신 contextBase.Server.MapPath. 그것을 조롱하게하고 싶습니까?

    interface IServerMapPath { 
        string MapPath(string virtualPath); 
    } 
    
    class ServerMapPath: IServerMapPath { 
        public string MapPath(string virtualPath) { 
         return HostingEnvironment.MapPath(virtualPath); 
        } 
    } 
    
    container.AddComponent<IServerMapPath, ServerMapPath>(); 
    

이 그런 다음 RegisterRoutesIServerMapPath를 주입, 예컨대 : 간단한 인터페이스를 통해 사용합니다.

+0

덕분에 몇 가지 해결 방법을 추가하여 업데이 트를 위해, 확실히 보일 것입니다. – Herman

관련 문제