When you try to create an object through the Ninject container, the exception Ninject : Object reference not set to an instance of an object. Tell me, please, what could be the problem?
Method from class NinjectDependencyResolver
private void AddBindings() { var mapperConfiguration = new MapperConfiguration(cfg => { cfg.AddProfile(new TagProfile()); }); var mapper = mapperConfiguration.CreateMapper(); _kernel.Bind<BlogDbContext>().ToSelf().InRequestScope(); _kernel.Bind<IRepository<Tag>, Repository<Tag>>(); _kernel.Bind<IMapper>().ToConstant(mapper); var repository = _kernel.Get<IRepository<Tag>>();// вылетает exception _kernel.Bind<ITagService, TagService>(); } Class Repository
public class Repository<T> : IRepository<T> where T : class { private readonly BlogDbContext _db; public Repository(BlogDbContext db) { _db = db; } } PS I don’t know if this is important, but the repository with its interface is in one build, and the registration of Ninject in another.
BlogDbContext. Let's call it EntityModel, then forNinjectwill be the next call_kernel.Bind<BlogDbContext>().To<EntityModel>().InRequestScope();- Vadim ProkopchukBlogDbContextis the class thatIdentityDbContextinherits. The context itself resolves without problems, but the repository does not. - Lightness_kernel.Bind<IRepository<Tag>, Repository<Tag>>();the correct declaration and then the string_kernel.Bind<ITagService, TagService>();? You for these classes do not bind implementation. I can assume that there should be_kernel.Bind<IRepository<Tag>>().To<Repository<Tag>>();- Vadim ProkopchukInRequestScope()for the context, do I need to do this for both the repository and the service? - Lightness