Specify Ninject Property for Private

I have a project using Ninject. I am creating an interface and implementing it in another class. Add below code to register service

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

      

Create a web page and write the following code

[Inject]
public IArticleContent _ArticleContentService { get; set; }

      

Write some extra code to get the data bound to the control and everything works .

Then I change the specified public announcement to closed

[Inject]
private IArticleContent _ArticleContentService { get; set; }

      

And get

Exception Details: System.NullReferenceException: Object reference not set to object instance.

After some research, I added

new StandardKernel(new NinjectSettings() { InjectNonPublic = true });

      

to a class NinjectWebCommon

in the Start method (method is also used RegisterServices

), but the same error. So after further research, it seems to me that I need to do it in a different way (i.e. Inside the constructor of my service class), or I need the code above elsewhere.

Can anyone please advise on how to use a property with Ninject when it sets a private or "correct" way to do it?

+3


source to share





All Articles