From 1cf5de03eca68b78ef8e89c0b67ce89a678df0a2 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Fri, 23 Aug 2019 22:33:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=95=B0=E6=8D=AE=E5=BA=93=E5=81=A5?= =?UTF-8?q?=E5=BA=B7=E6=A3=80=E6=9F=A5=E6=94=AF=E6=8C=81=20MongoDB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/HealthChecks/DBHealthCheck.cs | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) 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")); } } }