Skip to content

[API Proposal]: IConfigurationRoot should derive from IDisposable #86456

@adamsitnik

Description

@adamsitnik

Background and motivation

When working on #86146 I've realized that ConfigurationBuilder.Build returns an instance of IConfigurationRoot, which does not derive from IDisposable, but all implementations of IConfigurationRoot implement IDisposable. So the users need to cast the result returned by ConfigurationBuilder.Build to IDisposable and call Dispose themselves to cleanup the resources properly.

IConfigurationRoot config = new ConfigurationBuilder().AddXmlFile("settings.xml", false, true).Build();

(config as IDisposable)?.Dispose();

API Proposal

namespace 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 Usage

using IConfigurationRoot config = new ConfigurationBuilder().AddXmlFile("settings.xml", false, true).Build();

Alternative Designs

No response

Risks

Any custom implementations of IConfigurationRoot that don't implement IDisposable will have to implement it, moreover users of IConfigurationRoot 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions