|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Microsoft.Extensions.Configuration.Xml;
using Microsoft.Extensions.FileProviders;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Extension methods for adding <see cref="XmlConfigurationProvider"/>.
/// </summary>
public static class XmlConfigurationExtensions
{
/// <summary>
/// Adds the XML configuration provider at <paramref name="path"/> to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
[RequiresDynamicCode(XmlDocumentDecryptor.RequiresDynamicCodeMessage)]
[RequiresUnreferencedCode(XmlDocumentDecryptor.RequiresUnreferencedCodeMessage)]
public static IConfigurationBuilder AddXmlFile(this IConfigurationBuilder builder, string path)
{
return AddXmlFile(builder, provider: null, path: path, optional: false, reloadOnChange: false);
}
/// <summary>
/// Adds the XML configuration provider at <paramref name="path"/> to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <param name="optional"><see langword="true"/> if the file is optional.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
[RequiresDynamicCode(XmlDocumentDecryptor.RequiresDynamicCodeMessage)]
[RequiresUnreferencedCode(XmlDocumentDecryptor.RequiresUnreferencedCodeMessage)]
public static IConfigurationBuilder AddXmlFile(this IConfigurationBuilder builder, string path, bool optional)
{
return AddXmlFile(builder, provider: null, path: path, optional: optional, reloadOnChange: false);
}
/// <summary>
/// Adds the XML configuration provider at <paramref name="path"/> to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <param name="optional"><see langword="true"/> if the file is optional.</param>
/// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
[RequiresDynamicCode(XmlDocumentDecryptor.RequiresDynamicCodeMessage)]
[RequiresUnreferencedCode(XmlDocumentDecryptor.RequiresUnreferencedCodeMessage)]
public static IConfigurationBuilder AddXmlFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange)
{
return AddXmlFile(builder, provider: null, path: path, optional: optional, reloadOnChange: reloadOnChange);
}
/// <summary>
/// Adds an XML configuration source to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="provider">The <see cref="IFileProvider"/> to use to access the file.</param>
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <param name="optional"><see langword="true"/> if the file is optional.</param>
/// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
[RequiresDynamicCode(XmlDocumentDecryptor.RequiresDynamicCodeMessage)]
[RequiresUnreferencedCode(XmlDocumentDecryptor.RequiresUnreferencedCodeMessage)]
public static IConfigurationBuilder AddXmlFile(this IConfigurationBuilder builder, IFileProvider? provider, string path, bool optional, bool reloadOnChange)
{
ThrowHelper.ThrowIfNull(builder);
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(SR.Error_InvalidFilePath, nameof(path));
}
return builder.AddXmlFile(s =>
{
s.FileProvider = provider;
s.Path = path;
s.Optional = optional;
s.ReloadOnChange = reloadOnChange;
s.ResolveFileProvider();
});
}
/// <summary>
/// Adds an XML configuration source to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="configureSource">Configures the source.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
[RequiresDynamicCode(XmlDocumentDecryptor.RequiresDynamicCodeMessage)]
[RequiresUnreferencedCode(XmlDocumentDecryptor.RequiresUnreferencedCodeMessage)]
public static IConfigurationBuilder AddXmlFile(this IConfigurationBuilder builder, Action<XmlConfigurationSource>? configureSource)
=> builder.Add(configureSource);
/// <summary>
/// Adds an XML configuration source to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="stream">The <see cref="Stream"/> to read the XML configuration data from.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
[RequiresDynamicCode(XmlDocumentDecryptor.RequiresDynamicCodeMessage)]
[RequiresUnreferencedCode(XmlDocumentDecryptor.RequiresUnreferencedCodeMessage)]
public static IConfigurationBuilder AddXmlStream(this IConfigurationBuilder builder, Stream stream)
{
ThrowHelper.ThrowIfNull(builder);
return builder.Add<XmlStreamConfigurationSource>(s => s.Stream = stream);
}
}
}
|