From dbc776ddda21690e2829ccf52eed4f16185e5622 Mon Sep 17 00:00:00 2001 From: asesidaa <1061472754@qq.com> Date: Sun, 8 Oct 2023 15:49:55 +0800 Subject: [PATCH] Add validation to rank options --- Domain/Config/RankConfig.cs | 5 ++++- MainServer/Program.cs | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Domain/Config/RankConfig.cs b/Domain/Config/RankConfig.cs index b72623d..06ad211 100644 --- a/Domain/Config/RankConfig.cs +++ b/Domain/Config/RankConfig.cs @@ -1,8 +1,11 @@ -namespace Domain.Config; +using System.ComponentModel.DataAnnotations; + +namespace Domain.Config; public class RankConfig { public const string RANK_SECTION = "Rank"; + [Required, Range(1, double.MaxValue, ErrorMessage = "Value for {0} must be greater than 0!")] public int RefreshIntervalHours { get; set; } = 24; } \ No newline at end of file diff --git a/MainServer/Program.cs b/MainServer/Program.cs index 9dbb298..df33e65 100644 --- a/MainServer/Program.cs +++ b/MainServer/Program.cs @@ -52,8 +52,10 @@ try builder.Configuration.GetSection(GameConfig.GAME_SECTION)); builder.Services.Configure( builder.Configuration.GetSection(AuthConfig.AUTH_SECTION)); - builder.Services.Configure( - builder.Configuration.GetSection(RankConfig.RANK_SECTION)); + builder.Services.AddOptions() + .Bind(builder.Configuration.GetSection(RankConfig.RANK_SECTION)) + .ValidateDataAnnotations() + .ValidateOnStart(); var serverIp = builder.Configuration["ServerIp"] ?? "127.0.0.1"; var certificateManager = new CertificateService(serverIp, new SerilogLoggerFactory(Log.Logger).CreateLogger("")); @@ -82,6 +84,8 @@ try var refreshIntervalHours = builder.Configuration.GetSection(RankConfig.RANK_SECTION). GetValue("RefreshIntervalHours"); + refreshIntervalHours.Throw().IfLessThanOrEqualTo(0); + Log.Information("Rank refresh interval: {RefreshIntervalHours} hours", refreshIntervalHours); builder.Services.AddApplication(refreshIntervalHours); builder.Services.AddInfrastructure(builder.Configuration);