-
Notifications
You must be signed in to change notification settings - Fork 10.3k
After migrating to .NET 6 RC 1 No service for type Microsoft.Extensions.Configuration.IConfiguration can be found #36829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Why is extension method building the |
@davidfowl In .NET 5, I thought I needed to? |
So to be clear, this extension method is in a library I use both in .NET 5 and 6 |
Typically extension methods on
Calling That said, in this particular case of resolving |
Ok, so it's ok to do this but it's an anti-pattern, right? This is how I use the configuration and the arguments. From a best practice stand point, is it possible to make this code compliant with both .NET 5 and 6? public static IServiceCollection AddStrife(this IServiceCollection services, Action<DocumentStoreOptions> configureOptions) {
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
services.Configure<DocumentStoreOptions>(options => options.Settings = configuration.GetSection(nameof(DocumentStoreSettings))?.Get<DocumentStoreSettings>()).Configure(configureOptions)
return services;
} |
Like so. public static IServiceCollection AddStrife(this IServiceCollection services, Action<DocumentStoreOptions> configureOptions)
{
services.AddOptions<DocumentStoreOptions>()
.Configure<IConfiguration>((options, configuration) =>
{
options.Settings = configuration.GetSection(nameof(DocumentStoreSettings))?.Get<DocumentStoreSettings>();
})
.Configure(configureOptions);
return services;
} cc @IEvangelist we have a doc for guidance on how to build these sorts of libraries now right? |
Ok, thanks. Changed my configuration to your proposed solution. |
No problem. I'm gonna fix this anyway since it's a compatibility break. |
Yes sir, here you are @davidfowl |
In my
IServiceCollection
extension I have this code.This breaks ofter migrating to .NET 6 using the
Program.cs
only and noStartup
class.Do I need to register this in my extension as well? How can I make this work for .NET 5 and .NET 6?
The text was updated successfully, but these errors were encountered: