-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[API Proposal]: IConfigurationRoot should derive from IDisposable #86456
Comments
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsBackground and motivationWhen working on #86146 I've realized that IConfigurationRoot config = new ConfigurationBuilder().AddXmlFile("settings.xml", false, true).Build();
(config as IDisposable)?.Dispose(); API Proposalnamespace Microsoft.Extensions.Configuration;
- public interface IConfigurationRoot : IConfiguration
+ public interface IConfigurationRoot : IConfiguration, IDisposable
{
/// <summary>
/// Force the configuration values to be reloaded from the underlying <see cref="IConfigurationProvider"/>s.
/// </summary>
void Reload();
/// <summary>
/// The <see cref="IConfigurationProvider"/>s for this configuration.
/// </summary>
IEnumerable<IConfigurationProvider> Providers { get; }
} API Usageusing IConfigurationRoot config = new ConfigurationBuilder().AddXmlFile("settings.xml", false, true).Build(); Alternative DesignsNo response RisksAny custom implementations of Without introducing the mentioned changes we risk common resource leaks like in #86146.
|
I'm not sure the IConfigurationRoot is solely responsible for owning the FileProvider. Instead I think we should try to look at what's the best practice for lifetime management (and instance management) of the FileProvider. IMO it should be higher in the stack here. Might make sense to ask the original architects what they had in mind @davidfowl @eerhardt @halter73 |
My main goal of this proposal was to prevent similar resource leaks by letting the users know that IConfigurationRoot config = new ConfigurationBuilder().Build();
(config as IDisposable)?.Dispose(); // this is the problem In most cases it's probably fine, as the config usually lives as long as the app does. But it's definitely not 100% of all the use cases (a good example are test projects which may create a lot of such instances). |
My point was that I'm not convinced that I think a better solution is that we have a single |
Background and motivation
When working on #86146 I've realized that
ConfigurationBuilder.Build
returns an instance ofIConfigurationRoot
, which does not derive fromIDisposable
, but all implementations ofIConfigurationRoot
implementIDisposable
. So the users need to cast the result returned byConfigurationBuilder.Build
toIDisposable
and callDispose
themselves to cleanup the resources properly.API Proposal
API Usage
Alternative Designs
No response
Risks
Any custom implementations of
IConfigurationRoot
that don't implementIDisposable
will have to implement it, moreover users ofIConfigurationRoot
will most likely get a compiler warning about not disposing the instance.Without introducing the mentioned changes we risk common resource leaks like in #86146.
The text was updated successfully, but these errors were encountered: