diff --git a/src/Tgstation.Server.Host/Core/Application.cs b/src/Tgstation.Server.Host/Core/Application.cs index f9e3da3f18..b3e3286712 100644 --- a/src/Tgstation.Server.Host/Core/Application.cs +++ b/src/Tgstation.Server.Host/Core/Application.cs @@ -94,11 +94,11 @@ namespace Tgstation.Server.Host.Core services.AddSingleton(this); //configure configuration - services.Configure(configuration.GetSection(UpdatesConfiguration.Section)); - services.Configure(configuration.GetSection(DatabaseConfiguration.Section)); - services.Configure(configuration.GetSection(GeneralConfiguration.Section)); - services.Configure(configuration.GetSection(FileLoggingConfiguration.Section)); - services.Configure(configuration.GetSection(ControlPanelConfiguration.Section)); + services.UseStandardConfig(configuration); + services.UseStandardConfig(configuration); + services.UseStandardConfig(configuration); + services.UseStandardConfig(configuration); + services.UseStandardConfig(configuration); //enable options which give us config reloading services.AddOptions(); diff --git a/src/Tgstation.Server.Host/Core/ServiceCollectionExtensions.cs b/src/Tgstation.Server.Host/Core/ServiceCollectionExtensions.cs new file mode 100644 index 0000000000..87557989ce --- /dev/null +++ b/src/Tgstation.Server.Host/Core/ServiceCollectionExtensions.cs @@ -0,0 +1,44 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Globalization; +using Tgstation.Server.Host.Configuration; + +namespace Tgstation.Server.Host.Core +{ + /// + /// Extensions for + /// + static class ServiceCollectionExtensions + { + /// + /// Add a standard binding + /// + /// The to bind. Must have a const/static field named "Section" + /// The to configure + /// The containing the + /// + public static IServiceCollection UseStandardConfig(this IServiceCollection serviceCollection, IConfiguration configuration) where TConfig: class + { + if (serviceCollection == null) + throw new ArgumentNullException(nameof(serviceCollection)); + if (configuration == null) + throw new ArgumentNullException(nameof(configuration)); + + const string SectionFieldName = nameof(GeneralConfiguration.Section); + + var configType = typeof(TConfig); + var sectionField = configType.GetField(SectionFieldName); + if (sectionField == null) + throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "{0} has no {1} field!", configType, SectionFieldName)); + + var stringType = typeof(string); + if (sectionField.FieldType != stringType) + throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "{0} has invalid {1} field type, must be {2}!", configType, SectionFieldName, stringType)); + + var sectionName = (string)sectionField.GetValue(null); + + return serviceCollection.Configure(configuration.GetSection(sectionName)); + } + } +}