5

I am using asp.net-core v1.1.0 :)

I want to access the application settings values from a service class and not from a controller, my code is:

appsettings.json

// appsettings.json
{
  "ImagesDBSettings": {
    "Endpoint": "",
    "Key": "",
  }
}

Startup.cs

// Startup.cs
...
public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddOptions();
    services.Configure<ImagesDBSettings>(Configuration.GetSection("ImagesDBSettings"));
    ...
}
...

ImagesDBSettings.cs

// ImagesDBSettings.cs
public class ImagesDBSettings
{
    public string Endpoint { get; set; }
    public string Key { get; set; }
}

ImagesDBService.cs

// ImagesDBService.cs
public class ImagesDBService
{
    private readonly ImagesDBSettings _settings;
    public ImagesDBService(IOptions<ImagesDBSettings> settings)
    {
        _settings = settings.Value;
    }
}

On compiling I get the error:

There is no argument given that corresponds to the required formal parameter 'settings' of 'ImagesDBService.ImagesDBService(IOptions)'

Any ideas on how to make the Dependency Injection Work?

JohnnyAce
  • 3,569
  • 9
  • 37
  • 59

2 Answers2

5

IOptions dependancy will not be injected into the ImagesDBService with the code shown. You need to use AddTransient in startup.cs for that. For the DI outside controller see this question. Or you can pass IOptions to your service class from the controller (not the best option).

public IActionResult Index(IOptions<ImagesDBSettings> settings)
{
    ImagesDBService ss = new ImagesDBService(settings);
    return View();
}

Here is how I do it in my app without the DI. I have a static AppSettings class that I configure in ConfigureServices (startup.cs). I then simply have access to my AppSettings anywhere in the app.

public static class AppSettings
{
    public static string ConnectionString { get; set; }        
}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    AppSettings.ConnectionString = Configuration.GetConnectionString("DefaultConnection");
}
Community
  • 1
  • 1
Ross
  • 2,123
  • 1
  • 18
  • 18
  • 1
    At the end, I just gave up, the transient option still required to use the controller, what I was looking for (not explained in the question) was to access the Configuration file from a Class independent of any controller or Startup.cs – JohnnyAce Dec 22 '16 at 02:43
0

You said it's a compiling error so probably not something to do with DI, it's more likely you are missing the IOptions<> namespace.

If you haven't done it already, install package: Microsoft.Extensions.Options.ConfigurationExtensions from Nuget.

Then reference the namespace Microsoft​.Extensions​.Options to your class ImagesDBService

Klinger
  • 4,900
  • 1
  • 30
  • 35
  • The class already contains `using Microsoft.Extensions.Options;` :( – JohnnyAce Dec 21 '16 at 04:22
  • Is there any more code, or what you posted is the exact shape of your class? Does it inherit from some other class? – Klinger Dec 21 '16 at 04:36
  • Did you mean "on execution" instead of "on compiling"? – Klinger Dec 21 '16 at 04:41
  • Thanks for your help Klinger, is on compiling, on vscode I execute the "run" command and on the compiling step the compiler throws that error. ImagesDBServices doesn't inherit from another class. – JohnnyAce Dec 21 '16 at 05:43
  • The only additional thing I can think of is that you are missing a reference. – Klinger Dec 21 '16 at 05:49