Adding extension methods to IServiceCollection in ASP.NET Core I have the following extension method to add themes support to my application:

public static void AddThemes(this IServiceCollection services, Action&lt;ThemesOptions&gt; setup) {
    services.Configure(setup);
}


Where ThemesOptions is defined as:

public class ThemesOptions {
    public IEnumerable&lt;string&gt; Themes { get; set; }
}


Now in my application's startup ConfigureServices method I can say:

services.AddThemes(options =&gt; {
    options.Themes = Configuration.GetSection("Themes").Get&lt;ThemesOptions&gt;().Themes;
});


I'm not sure that I like that I have to set every property for the options. Alternatively I tried:

services.AddThemes(options =&gt; {
    options = Configuration.GetSection("Themes").Get&lt;ThemesOptions&gt;();
});


And:

services.AddThemes(options =&gt; Configuration.GetSection("Themes"));


However when I inject IOptions&lt;ThemesOptions&gt; the Themes property is null.

An alternative to I changed my extension method to:

public static void AddThemes(this IServiceCollection services, IConfiguration configuration) {
    services.Configure&lt;ThemesOptions&gt;(configuration.GetSection("Themes"));
}


Now I can say the following within my application's Startup ConfigureServices method:

services.AddThemes(Configuration);


This worked fine, however, I feel the problem with this approach is that the extension method only allows the options to be set from the configuration.

I'd appreciate it if someone could confirm whether my first solution is correct and if it can be improved upon.
Instead of doing 

services.AddThemes(options =&gt; {
    options = Configuration.GetSection("Themes").Get&lt;ThemesOptions&gt;();
});


you could  use

services.AddThemes(options =&gt; {
    Configuration.GetSection("Themes").Bind(options);
});


which will programatically set each value of options based on what's in the configuration.