diff --git a/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs index 51f7efed..3ec99cd7 100644 --- a/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs +++ b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs @@ -1,6 +1,7 @@ using Bootstrap.DataAccess; -using Bootstrap.Security; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Diagnostics.HealthChecks; +using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -14,6 +15,20 @@ namespace Bootstrap.Admin.HealthChecks /// public class DBHealthCheck : IHealthCheck { + private IConfiguration _configuration; + private static readonly Func ConnectionStringResolve = (c, name) => string.IsNullOrEmpty(name) + ? c.GetSection("ConnectionStrings").GetChildren().FirstOrDefault()?.Value + : c.GetConnectionString(name); + + /// + /// 构造函数 + /// + /// + public DBHealthCheck(IConfiguration configuration) + { + _configuration = configuration; + } + /// /// 异步检查方法 /// @@ -22,18 +37,23 @@ namespace Bootstrap.Admin.HealthChecks /// public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { - using (var db = DbManager.Create()) - { - var connStr = db.ConnectionString; - var dicts = db.Fetch("Select * from Dicts"); - var data = new Dictionary() + var db = _configuration.GetSection("DB").GetChildren() + .Select(config => new { - { "ConnectionString", connStr }, - { "DbType", db.Provider.GetType().Name }, - { "Dicts", dicts.Count } - }; - return dicts.Any() ? Task.FromResult(HealthCheckResult.Healthy("Ok", data)) : Task.FromResult(HealthCheckResult.Degraded("No init data in DB")); - } + Enabled = bool.TryParse(config["Enabled"], out var en) ? en : false, + ProviderName = config["ProviderName"], + Widget = config["Widget"], + ConnectionString = ConnectionStringResolve(config.GetSection("ConnectionStrings").Exists() ? config : _configuration, string.Empty) + }).FirstOrDefault(i => i.Enabled); + var dicts = DictHelper.RetrieveDicts(); + var data = new Dictionary() + { + { "ConnectionString", db.ConnectionString }, + { "Widget", db.Widget }, + { "DbType", db.ProviderName }, + { "Dicts", dicts.Count() } + }; + return dicts.Any() ? Task.FromResult(HealthCheckResult.Healthy("Ok", data)) : Task.FromResult(HealthCheckResult.Degraded("No init data in DB")); } } }