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?

1
  • do you have any types derived from ImagesDBService? Commented Dec 21, 2016 at 4:48

2 Answers 2

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");
}
Sign up to request clarification or add additional context in comments.

1 Comment

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
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

5 Comments

The class already contains using Microsoft.Extensions.Options; :(
Is there any more code, or what you posted is the exact shape of your class? Does it inherit from some other class?
Did you mean "on execution" instead of "on compiling"?
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.
The only additional thing I can think of is that you are missing a reference.

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.