0

I have the following inside my ConfigureServices method in the Startup class. I am adding a singleton instance of ToastNotification which is an implementation of IToastNotification.

public void ConfigureServices(IServiceCollection services)
{
   services.AddInstance<IToastNotification>(new ToastNotification()
         {
         });
}

The problem is that when I watch the value of services while debugging at the end of this method, the implementation type of the IToastNotification service is null. Therefore, when I try to get the Toastnotification instance from the services collection in the controllers, it is always null.

This is how I am getting the Toastnotification using dependency injection

[FromServices]
private IToastNotification ToastNotification { get; set; }

What am I doing wrong?

2 Answers 2

3

Don't use property injection, it has been removed and won't work in the future versions: https://github.com/aspnet/Announcements/issues/115.

Instead, consider using constructor dependency injection.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I did what you said. I guess I need to keep a close eye on all the breaking changes.
0

Using constructor dependency injection, you can do thing like that:

// Register your service.
services.AddInstance<IToastNotification>(new ToastNotification());

And in your Controller:

public class HomeController : Controller
{
    private readonly IToastNotification _toastNotification;

    public HomeController(IToastNotification toastNotification)
    {
        _toastNotification = toastNotification;
    }

    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.