gpt4 book ai didi

ninject - 在我的 MVC3 中使用 Ninject 时,我得到一个 Ninject.ActivationException : Error activating IRepository{Ranking} installed via Nuget

转载 作者:行者123 更新时间:2023-12-02 02:17:20 26 4
gpt4 key购买 nike

我通过 nuget 安装了 Ninject,并在 NinjectMVC3 的 RegisterServices 方法(由 nuget 创建)中注册了我的绑定(bind)。按照我的代码:

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));
}

这是我的家庭 Controller :

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

当我执行 HomeController 时,出现以下异常:

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.

我做错了什么?

最佳答案

您是否创建了默认模块来设置绑定(bind)而不是在注册服务方法中?

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

然后在您的 Global.asax 文件中执行此操作:

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

kernel.Load(Assembly.GetExecutingAssembly());

return kernel;
}

关于ninject - 在我的 MVC3 中使用 Ninject 时,我得到一个 Ninject.ActivationException : Error activating IRepository{Ranking} installed via Nuget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9880037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com