/// <summary> /// Adds the JSON configuration provider at <paramref name="path"/> to <paramref name="configurationBuilder"/>. /// </summary> /// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param> /// <param name="path">Absolute path or path relative to <see cref="IConfigurationBuilder.BasePath"/> of /// <paramref name="configurationBuilder"/>.</param> /// <param name="optional">Determines if loading the configuration provider is optional.</param> /// <returns>The <see cref="IConfigurationBuilder"/>.</returns> /// <exception cref="ArgumentException">If <paramref name="path"/> is null or empty.</exception> /// <exception cref="FileNotFoundException">If <paramref name="optional"/> is <c>false</c> and the file cannot /// be resolved.</exception> public static IConfigurationBuilder AddIniFile( this IConfigurationBuilder configurationBuilder, string path, bool optional) { if (configurationBuilder == null) { throw new ArgumentNullException(nameof(configurationBuilder)); } if (string.IsNullOrEmpty(path)) { throw new ArgumentException(Resources.Error_InvalidFilePath, nameof(path)); } var fullPath = Path.Combine(configurationBuilder.GetBasePath(), path); if (!optional && !File.Exists(fullPath)) { throw new FileNotFoundException(Resources.FormatError_FileNotFound(fullPath), fullPath); } configurationBuilder.Add(new IniConfigurationProvider(fullPath, optional: optional)); return(configurationBuilder); }
public void JsonConfiguration_Throws_On_Missing_Configuration_File() { var configSource = new JsonConfigurationProvider("NotExistingConfig.json", optional: false); var exception = Assert.Throws <FileNotFoundException>(() => configSource.Load()); // Assert Assert.Equal(Resources.FormatError_FileNotFound("NotExistingConfig.json"), exception.Message); }
/// <summary> /// Adds the JSON configuration source at <paramref name="path"/> to <paramref name="configuraton"/>. /// </summary> /// <param name="configuration">The <see cref="IConfigurationBuilder"/> to add to.</param> /// <param name="path">Absolute path or path relative to <see cref="IConfigurationBuilder.BasePath"/> of /// <paramref name="configuration"/>.</param> /// <param name="optional">Determines if loading the configuration source is optional.</param> /// <returns>The <see cref="IConfigurationBuilder"/>.</returns> /// <exception cref="ArgumentException">If <paramref name="path"/> is null or empty.</exception> /// <exception cref="FileNotFoundException">If <paramref name="optional"/> is <c>false</c> and the file cannot /// be resolved.</exception> public static IConfigurationBuilder AddIniFile( [NotNull] this IConfigurationBuilder configuration, string path, bool optional) { if (string.IsNullOrEmpty(path)) { throw new ArgumentException(Resources.Error_InvalidFilePath, nameof(path)); } var fullPath = ConfigurationHelper.ResolveConfigurationFilePath(configuration, path); if (!optional && !File.Exists(fullPath)) { throw new FileNotFoundException(Resources.FormatError_FileNotFound(fullPath), fullPath); } configuration.Add(new IniFileConfigurationSource(fullPath, optional: optional)); return(configuration); }