Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core 2 Unable to resolve service for type

I'm attempting to register my own custom options. I have, in my ASP.Net project (Kimble.API), an appsettings.json file. It looks like this:

{
    "NotificationHub": {
        "AccountName": "my-notification-hub-name",
        "ConnectionString": "my-secret-connection-string"
    }
}

In my .Net Standard library (Kimble.Core), which the API project references, I have a class NotificationHubOptions:

public class NotificationHubOptions
{
    public string AccountName { get; set; }
    public string ConnectionString { get; set; }
}

Back to the API project.

In my Startup.cs file, in the ConfigureServices method, I register the options:

services.Configure<NotificationHubOptions>(configuration.GetSection("NotificationHub"));

I've checked, and the registration does show up in the services collection.

My controller's constructor looks like this:

public MyController(NotificationHubOptions options)
{
    _notificationHubOptions = options;
}

However, when I try to call a method on the controller, I always get an exception:

System.InvalidOperationException: 'Unable to resolve service for type 'Kimble.Core.Options.NotificationHubOptions' while attempting to activate 'Kimble.API.Controllers.MyController'.'

This all worked before I moved the NotificationHubOptions class to my Core project. However, I can't see why that should matter at all.

like image 673
Shawn Beachy Avatar asked Jan 31 '18 19:01

Shawn Beachy


People also ask

What is IHttpContextAccessor?

HTTP context accessor. Finally, you can use the IHttpContextAccessor helper service to get the HTTP context in any class that is managed by the ASP.NET Core dependency injection system. This is useful when you have a common service that is used by your controllers.

What is IServiceCollection in .NET Core?

} IServiceCollection is the collection of the service descriptors. We can register our services in this collection with different lifestyles (Transient, scoped, singleton) IServiceProvider is the simple built-in container that is included in ASP.NET Core that supports constructor injection by default.

What is IDesignTimeDbContextFactory?

IDesignTimeDbContextFactory<TContext> InterfaceImplement this interface to enable design-time services for context types that do not have a public default constructor. At design-time, derived DbContext instances can be created in order to enable specific design-time experiences such as Migrations.

Where is IWebHostEnvironment?

IWebHostEnvironment is included in the Microsoft. AspNetCore. Hosting package, you simply need to add it as a reference to your project by right clicking on your project file and selecting 'Manage Nuget Packages' then searching for Microsoft.


1 Answers

You need to inject IOptions<TOptions>, like so:

public MyController(IOptions<NotificationHubOptions> options)
{
    _notificationHubOptions = options.Value;
}

When you use Configure, you are registering a callback to configure the options instance for that type when it creates it. In this case using the configuration section to bind data to the options object.

So the options class itself is not in DI.

If you wanted that you could do this:

var options = Configuration.GetSection("NotificationHub").Get<NotificationHubOptions>();
services.AddSingleton<NotificationHubOptions>(options);
like image 150
juunas Avatar answered Sep 23 '22 17:09

juunas