From 09a953704446bad95510ff00f3b1fde96aa6e2cf Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Fri, 2 Nov 2018 10:25:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=9AMo?= =?UTF-8?q?ngoDB=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E5=8F=98=E5=8C=96?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E9=87=8D=E6=96=B0=E8=8E=B7=E5=8F=96=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E8=BF=9E=E6=8E=A5=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MongoDbAccessManager.cs | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs index ce8d4b90..cbdbd4dd 100644 --- a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs +++ b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs @@ -1,5 +1,7 @@ using Bootstrap.Security; +using Longbow.Configuration; using Longbow.Data; +using Microsoft.Extensions.Primitives; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; @@ -16,7 +18,7 @@ namespace Bootstrap.DataAccess.MongoDB { private static IMongoDatabase _db = null; private static bool _register = false; - + private static readonly object _locker = new object(); /// /// /// @@ -26,18 +28,22 @@ namespace Bootstrap.DataAccess.MongoDB { if (_db == null) { - if (!_register) + lock (_locker) { - _register = true; - DbAdapterManager.RegisterConfigChangeCallback("MongoDB", InitDb); + if (!_register) + { + _register = true; + ChangeToken.OnChange(() => ConfigurationManager.AppSettings.GetReloadToken(), () => _db = null); + InitClassMap(); + } + InitDb(); } - InitDb(); - InitClassMap(); } return _db; } } + #region Collections /// /// /// @@ -113,6 +119,7 @@ namespace Bootstrap.DataAccess.MongoDB return DBAccess.GetCollection("Navigations"); } } + #endregion private static void InitDb() { @@ -120,7 +127,7 @@ namespace Bootstrap.DataAccess.MongoDB if (string.IsNullOrEmpty(connectString)) throw new InvalidOperationException("Please set the BA default value in configuration file."); var seq = connectString.Split(";", StringSplitOptions.RemoveEmptyEntries); - if (seq.Length != 2) return; + if (seq.Length != 2) throw new InvalidOperationException("ConnectionString invalid in configuration file. It should be mongodb://127.0.0.1:27017;Data Source=BootstrapAdmin"); var client = new MongoClient(seq[0]); _db = client.GetDatabase(seq[1].Split("=", StringSplitOptions.RemoveEmptyEntries).LastOrDefault());