2014-01-30 2 views
0

과 함께 실패합니다. 다음과 같은 간단한 ApiController 예제가 있습니다.Ninject 바인딩이 ApiController에서 "Ninject 구성 요소 ICache로드 오류"

로드 오류 Ninject에 구성 요소 ICACHE 이러한 구성 요소는 커널의 구성 요소 컨테이너에 등록되지 않은 :이 나에게 오류를 제공

public class TestAPIController : ApiController 
{ 
    public TestAPIController(IKernel kernel) { } 

    [HttpGet] 
    public string Test() 
    { 
     return "success! " + DateTimeOffset.Now.ToString("F"); 
    } 
} 

.

Ninject.WebApi.DependencyResolver 패키지를 설치했지만 여전히 실패하고 있습니다. 사용

private static IKernel CreateKernel() 
    {   
     var kernel = new StandardKernel(new VBNinjectModule()); 
     kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 

     kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

     //GlobalConfiguration.Configuration.DependencyResolver = new VBNinjectDependencyResolver(kernel); 
     GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel); 
     return kernel; 
    } 

: 여기

내 CreateKernel의 NinjectWebCommon의 클래스입니다

Ninject에 3.0.1.10

Ninject.MVC3 3.0.0.6

Ninject.Web.Common : 3.0 .0.7

Ninject.WebApi.DependencyResolver 0.1.4758.24814

도움을 미리 감사드립니다.

건배!

+1

ninject가 IKernel을 자동 바인딩 (또는 IResolutionRoot가 맞습니까?)했기 때문에 바인드 할 필요가 없습니다. Func 팩토리의 인젝션은 자동으로 작동합니다. 어쨌든 .Bind >() (IFoo 바인딩이있는 한)을 지정할 필요가 없습니다. 또한 IResolutionRoot를 주입 해보십시오. – BatteryBackupUnit

답변

0

컨트롤러에 IKernel을 주입하고 싶지 않습니다. 대신 서비스를 등록하고 해당 서비스를 컨트롤러에 주입하려고합니다.

private static IKernel CreateKernel() 
{ 
    var kernel = new StandardKernel(); 
    kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
    kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

    RegisterServices(kernel); 

    GlobalConfiguration.Configuration.DependencyResolver = 
     new NinjectDependencyResolver(kernel); 

    return kernel; 
} 

private static void RegisterServices(IKernel kernel) 
{ 
    kernel.Bind<IMyService>().To<MyService>().InRequestScope(); 
}  

// Your api controller 
public class TestAPIController : ApiController 
{ 
    private readonly IMyService _myService ; 

    public TestAPIController(IMyService myService) 
    { 
     _myService = myService; 
    } 
} 
+0

승리, 응답 주셔서 감사합니다. 우리는 VBNinjectModule() 클래스 내부의 모든 것을 바인딩합니다. 내가 직접 IKernel을 원한다면, 내 코드 중 일부는 리플렉션을 사용하여 객체를 인스턴스화하고 Activator.CreateInstance()를 사용하는 대신 kernel.Get ()을 사용하여 MyService의 모든 종속성도 주입되도록 할 수 있습니다. – jhilden

+0

IKernel은 인스턴스화를 위해 IBindingRoot 및 IResolutionRoot를 구현합니다. IResolutionRoot로 충분합니다. – BatteryBackupUnit

+0

좋은 제안, 나는 IResolutionRoot를 생성자에 넣으려고했지만 같은 오류가 발생합니다. – jhilden