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.

  • Do you use EntityFramework? In theory, you should have a class that inherits BlogDbContext . Let's call it EntityModel, then for Ninject will be the next call _kernel.Bind<BlogDbContext>().To<EntityModel>().InRequestScope(); - Vadim Prokopchuk
  • @VadimProkopchuk, BlogDbContext is the class that IdentityDbContext inherits. The context itself resolves without problems, but the repository does not. - Lightness
  • Wait, but is it _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 Prokopchuk
  • @VadimProkopchuk, Thanks, that was the problem! Maybe you know the answer to another question: if I do InRequestScope() for the context, do I need to do this for both the repository and the service? - Lightness
  • @Lightness, by the way, doing a BlogDbContext with an InRequestScope lifetime is a bad idea. BlogDbContext (and generally the context in EF) is the implementation of the Repository + Unit Of Work. Unit Of Work has to live exactly one business operation, and this is not exactly the same as “one http request”. The problem will shoot, for example, when trying to write an error when processing a request to the log in the database. Or on methods like "import several records" - one context will force you to work according to the principle "either all records are imported, or none at all". - PashaPash

1 answer 1

The error indicates that an object reference is not specified in the object instance.

Object reference not set.

In these two lines

 _kernel.Bind<IRepository<Tag>, Repository<Tag>>(); _kernel.Bind<ITagService, TagService>(); 

You "tell" the IoC container that when requesting an IRepository<Tag> or Repository<Tag> , you must return the implementation, but do not specify it.

The next call will be correct.

 _kernel.Bind<IRepository<Tag>>().To<Repository<Tag>>(); _kernel.Bind<ITagService>().To<TagService>(); 

You tell the IoC container that when IRepository<Tag> requested to create an instance of the Repository<Tag> class. For the second line is similar.

And the answer to the question from the comment :

for context I do InRequestScope (), do I need to do this for both the repository and the service?

For the repository and the Service do not need to do InRequestScope() , only for the context BlogDbContext


UPD

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.

Not important, the main thing is to specify a link to the assembly in which the repository and interfaces lie