1
votes

There is a stackoverflow answer here on how to create strongly typed configuration on a .net core console app but my question is how do you pass this strongly typed configuration to other services;

something like this

static void Main(string[] args)
{
    //...
    .AddSingleton<IMyService, MyService>()
}

public class MyService
{
    private readonly IConfiguration _configuration;
    public MyService (IConfiguration configuration)
    {
        _configuration = configuration;
    }
}
1
check thisEldar
Through servicecollection extentions, which you'll have access to by referencing the class libraries containing them.Ali Reza Dehdar

1 Answers

0
votes

Here's an example from MS documentation https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1#accessing-options-during-startup

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOptions<MyConfigOptions>()
            .Bind(Configuration.GetSection(MyConfigOptions.MyConfig))
            .ValidateDataAnnotations();

        services.AddControllersWithViews();
    }

And consumer:

public HomeController(IOptions<MyConfigOptions> config,
                          ILogger<HomeController> logger)