Parse config value correctly

This commit is contained in:
Mark Monteiro 2020-03-11 22:55:10 +01:00
parent 29bad073eb
commit d437950ac3
2 changed files with 2 additions and 16 deletions

View File

@ -31,7 +31,7 @@ namespace MediaBrowser.Controller.Extensions
/// <returns>The parsed config value.</returns>
/// <exception cref="FormatException">The config value is not a valid bool string. See <see cref="bool.Parse(string)"/>.</exception>
public static bool IsNoWebContent(this IConfiguration configuration)
=> configuration.ParseBoolean(NoWebContentKey);
=> configuration.GetValue<bool>(NoWebContentKey);
/// <summary>
/// Retrieves the FFmpeg probe size from the <see cref="IConfiguration" />.
@ -48,20 +48,5 @@ namespace MediaBrowser.Controller.Extensions
/// <returns>The FFmpeg analyse duration option.</returns>
public static string GetFFmpegAnalyzeDuration(this IConfiguration configuration)
=> configuration[FfmpegAnalyzeDurationKey];
/// <summary>
/// Convert the specified configuration string value its <see cref="bool"/> equivalent.
/// </summary>
/// <param name="configuration">The configuration to retrieve and parse the setting from.</param>
/// <param name="key">The key to use to retrieve the string value from the configuration.</param>
/// <returns>The parsed boolean value.</returns>
/// <exception cref="FormatException">The config value is not a valid bool string. See <see cref="bool.Parse(string)"/>.</exception>
public static bool ParseBoolean(this IConfiguration configuration, string key)
{
string configValue = configuration[key];
return bool.TryParse(configValue, out bool result) ?
result :
throw new FormatException($"Invalid value for configuration option '{key}' (expected a boolean): {configValue}");
}
}
}

View File

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.1" />
</ItemGroup>
<ItemGroup>