Search code examples
asp.net-coredependency-injectionasp.net-core-2.1.net-4.7.1

Simplified approach to IOptions<T>


I am trying to get a .NET Framework class library in line with an ASP.NET Core 2.1 application while using builtin DI mechanism. Now, I created a config class and added appropriate section to appsettings.json:

services.Configure<MyConfig>(Configuration.GetSection("MyConfiguration"));
services.AddScoped<MyService>();

In class lib:

public class MyService 
{
    private readonly MyConfig _config;

    public MyService(IOptions<MyConfig> config)
    {
        _config = config.Value;
    }
}

However, in order to build this classlib I have to add Microsoft.Extensions.Options NuGet package. The problem is that package carries a hell of a lot of dependencies which seem rather excessive to add just for the sake of one interface.

enter image description here

So, the question ultimately is, "is there another approach I can take to configure a DI service located in .NET Framework class library which is not dependency heavy?


Solution

  • Check this article written by Filip Wojcieszyn.

    https://www.strathweb.com/2016/09/strongly-typed-configuration-in-asp-net-core-without-ioptionst/

    You add extension method:

    public static class ServiceCollectionExtensions
    {
        public static TConfig ConfigurePOCO<TConfig>(this IServiceCollection services, IConfiguration configuration) where TConfig : class, new()
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            if (configuration == null) throw new ArgumentNullException(nameof(configuration));
    
            var config = new TConfig();
            configuration.Bind(config);
            services.AddSingleton(config);
            return config;
        }
    }
    

    Apply it in configuration:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.ConfigurePOCO<MySettings>(Configuration.GetSection("MySettings"));
    }
    

    And then use it:

    public class DummyService
    {
        public DummyService(MySettings settings)
        {
            //do stuff
        }
    }