2012-03-26 2 views
3

Ich installiert Ninject über Nuget, und ich registriert meine Bindungen in NinjectMVC3's RegisterServices Methode (erstellt von nuget). Folgen Sie meinem Code:Bei der Verwendung von Ninject in meinem MVC3 bekomme ich eine Ninject.ActivationException: Fehler beim Aktivieren von IRepository {Ranking} über Nuget

private static void RegisterServices(IKernel kernel) 
{ 
    kernel.Bind<IUnitOfWork>().To<UnitOfWork>(); 
    kernel.Bind<IRepository<Action>>().To<ActionRepository>(); 
    kernel.Bind<IRepository<ActionType>>().To<ActionTypeRepository>(); 
    kernel.Bind<IRepository<City>>().To<CityRepository>(); 
    kernel.Bind<IRepository<Country>>().To<CountryRepository>(); 
    kernel.Bind<IRepository<Goods>>().To<GoodsRepository>(); 
    kernel.Bind<IRepository<Media>>().To<MediaRepository>(); 
    kernel.Bind<IRepository<MediaType>>().To<MediaTypeRepository>(); 
    kernel.Bind<IRepository<Ranking>>().To<RankingRepository>(); 
    kernel.Bind<IRepository<Role>>().To<RoleRepository>(); 
    kernel.Bind<IRepository<Sponsor>>().To<SponsorRepository>(); 
    kernel.Bind<IRepository<State>>().To<StateRepository>(); 
    kernel.Bind<IRepository<UserAccountInfo>>().To<UserAccountInfoRepository>(); 
    kernel.Bind<IRepository<UserAction>>().To<UserActionRepository>(); 
    kernel.Bind<IRepository<UserDeservesGoods>>().To<UserDeservesGoodsRepository>(); 
    kernel.Bind<IRepository<UserGoods>>().To<UserGoodsRepository>(); 
    kernel.Bind<IRepository<User>>().To<UserRepository>(); 
    kernel.Bind<IUserService>().To<UserService>(); 
    kernel.Bind<IAccountService>().To<AccountService>(); 

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

hier ist mein Homecontroller:

readonly IRepository<Ranking> repoRanking; 
public HomeController(IRepository<Ranking> repoRanking) 
{ 
    this.repoRanking = repoRanking; 
} 

Als ich Homecontroller ausführen, bekomme ich folgen Ausnahme:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: Ninject.ActivationException: Error activating IRepository{Ranking} 
No matching bindings are available, and the type is not self-bindable. 
Activation path: 
2) Injection of dependency IRepository{Ranking} into parameter repoRanking of constructor of type HomeController 
1) Request for HomeController 

Suggestions: 
1) Ensure that you have defined a binding for IRepository{Ranking}. 
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 
3) Ensure you have not accidentally created more than one kernel. 
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 
5) If you are using automatic module loading, ensure the search path and filters are correct. 

Was ich falsch?

Antwort

2

Haben Sie ein Standardmodul erstellt, in dem Sie Ihre Bindungen statt in der Registerdienstmethode festlegen?

public class DefaultModule : NinjectModule { 
    public override void Load() { 
     Bind<IProductService>().To<ProductService>().InTransientScope(); 
    } 
} 

und dann in Ihrer Datei Global.asax dies zu tun:

protected override IKernel CreateKernel() { 
    var kernel = new StandardKernel(); 

    kernel.Load(Assembly.GetExecutingAssembly()); 

return kernel; 
} 
+0

Hallo @ Josh, die RegisterServices Methode bereits meine Bindungen eingestellt. Jedenfalls habe ich deinen Vorschlag getestet und nicht funktioniert. –

+0

Hallo, ich habe mein Problem gelöst, ich habe meine DependencyResolver zweimal eingestellt. Ich habe in Global.asax entfernt und mein Code hat richtig funktioniert. Vielen Dank –

Verwandte Themen