2011-12-02 2 views
6

나는이Httpcontext.Session는 위해 Application_Start 방법이라고 Ninject에

private void RegisterDependencyResolver() 
{ 
    HttpContextBase context = new HttpContextWrapper(HttpContext.Current); 
    var kernel = new StandardKernel(); 
    kernel.Bind<ISession>().To<SessionService>() 
          .InRequestScope() 
          .WithConstructorArgument("context", ninjectContext => context); 

    DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel)); 
} 

RegisterDependencyResolver처럼 Ninject에 사용 HttpContext를 주입하고() 항상 null입니다.

이 인터페이스는 세션을 처리하는 클래스의 생성자에 삽입됩니다.

세션이 초기화되지 않으므로 아무 것도 추가 할 수 없다는 문제가 있습니다.

context.session [ "something"] = "something"과 같은 코드는 null 참조 예외를 발생시킵니다.

Application_Start가 수명주기에 너무 일찍 있습니까? 내가 생각 .InRequestScope()이 수정하지만 그것은 나를 위해 작동하지 않습니다.

+0

응용 프로그램이 시작될 때 세션이 없습니다. – Amy

+0

https://github.com/ninject/ninject.web.mvc/blob/Maintenance_2.2/mvc3/src/Ninject.Web.Mvc/Bootstrapper.cs 행 : 68 및 69 –

답변

9

IIS 통합 모드에서 실행중인 경우 Application_Start의 HTTP 컨텍스트 개체에 액세스 할 수 없습니다.

는 다음과 같이하십시오 :

private void RegisterDependencyResolver() 
{ 
    kernel 
     .Bind<ISession>() 
     .To<SessionService>() 
     .InRequestScope() 
     .WithConstructorArgument(
      "context", 
      ninjectContext => new HttpContextWrapper(HttpContext.Current) 
     ); 

    DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel)); 
}