From bb1eeed231bb2d85140f88537d77a6103de0ac16 Mon Sep 17 00:00:00 2001 From: "chris.watts90@outlook.com" Date: Thu, 2 Mar 2017 22:04:58 +0000 Subject: [PATCH] reference ConfigurationHandler and update calls to get configurations. removed ConfigMonitor.cs. #54 --- .../CardReaderService/CardReaderService.cs | 7 +- .../CardReaderService.csproj | 6 +- .../CardReaderService/DataCenterHelper.cs | 2 +- .../NLogLogger/NLogLogger.csproj | 4 + .../WindowsDataCenter/NLogLogger/NLogger.cs | 4 +- .../SQLiteRepository/SQLiteRepository.cs | 4 +- .../SQLiteRepository/SQLiteRepository.csproj | 4 + .../WindowsDataCenter/ConfigMonitor.cs | 93 ------------------- .../Controllers/ApplicationController.cs | 5 +- .../Controllers/UsersController.cs | 2 +- .../WindowsDataCenter/DataCenterService.cs | 10 +- .../WindowsDataCenter/StartOwin.cs | 2 - .../WindowsDataCenter.csproj | 5 +- 13 files changed, 25 insertions(+), 123 deletions(-) delete mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/ConfigMonitor.cs diff --git a/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.cs b/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.cs index ce0f383..9422698 100644 --- a/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.cs +++ b/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.cs @@ -19,7 +19,6 @@ namespace CardReaderService private SCardMonitor _cardMonitor; private ILogger _logger; - private ConfigMonitor.ConfigMonitor _cfgMonitor; public CardReaderService() { @@ -38,8 +37,6 @@ namespace CardReaderService _logger = NinjectHelper.GetInstance().Get(); _logger.Trace("Starting Service.. Getting available readers"); - _cfgMonitor = new ConfigMonitor.ConfigMonitor(configPath); - var ctxFactory = ContextFactory.Instance; using(var context = ctxFactory.Establish(SCardScope.System)) { @@ -54,7 +51,7 @@ namespace CardReaderService _logger.Trace("Found reader: {0}", reader); } - var readerNameConfig = ConfigurationManager.AppSettings["ReaderName"]; + var readerNameConfig = ConfigurationHandler.ConfigurationHandler.GetConfiguration("ReaderName"); if (string.IsNullOrEmpty(readerNameConfig)) { if (!readerNames.Contains(readerNameConfig)) @@ -83,8 +80,6 @@ namespace CardReaderService protected override void OnStop() { - _cfgMonitor.Stop(); - _stopMainWorkerThread = true; if (_mainWorkThread!= null && _mainWorkThread.IsAlive) { diff --git a/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.csproj b/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.csproj index 3a2a693..ffb3c89 100644 --- a/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.csproj +++ b/DataCenter_Windows/WindowsDataCenter/CardReaderService/CardReaderService.csproj @@ -80,9 +80,9 @@ - - {6f3878b0-1e07-4de7-bfec-509ff4443b71} - ConfigMonitor + + {115250F6-F8C4-4F9B-A15F-251EA258D963} + ConfigurationHandler {B7347B72-E208-423A-9D99-723B558EA3D7} diff --git a/DataCenter_Windows/WindowsDataCenter/CardReaderService/DataCenterHelper.cs b/DataCenter_Windows/WindowsDataCenter/CardReaderService/DataCenterHelper.cs index 6180064..6bb0452 100644 --- a/DataCenter_Windows/WindowsDataCenter/CardReaderService/DataCenterHelper.cs +++ b/DataCenter_Windows/WindowsDataCenter/CardReaderService/DataCenterHelper.cs @@ -13,7 +13,7 @@ namespace CardReaderService { public static void Post(CardDataPost postObject, string url) { - var endpointConfig = ConfigurationManager.AppSettings["DataCenterServiceEndpoint"] ?? + var endpointConfig = ConfigurationHandler.ConfigurationHandler.GetConfiguration("DataCenterServiceEndpoint") ?? "http://localhost:8800"; using (var client = new HttpClient()) diff --git a/DataCenter_Windows/WindowsDataCenter/NLogLogger/NLogLogger.csproj b/DataCenter_Windows/WindowsDataCenter/NLogLogger/NLogLogger.csproj index cef047d..a87fe54 100644 --- a/DataCenter_Windows/WindowsDataCenter/NLogLogger/NLogLogger.csproj +++ b/DataCenter_Windows/WindowsDataCenter/NLogLogger/NLogLogger.csproj @@ -49,6 +49,10 @@ + + {115250F6-F8C4-4F9B-A15F-251EA258D963} + ConfigurationHandler + {B7347B72-E208-423A-9D99-723B558EA3D7} Interfaces diff --git a/DataCenter_Windows/WindowsDataCenter/NLogLogger/NLogger.cs b/DataCenter_Windows/WindowsDataCenter/NLogLogger/NLogger.cs index 7132a7e..291b4a9 100644 --- a/DataCenter_Windows/WindowsDataCenter/NLogLogger/NLogger.cs +++ b/DataCenter_Windows/WindowsDataCenter/NLogLogger/NLogger.cs @@ -1,5 +1,4 @@ using System; -using System.Configuration; using System.IO; using System.Reflection; using NLog; @@ -13,7 +12,8 @@ namespace NLogLogger private NLog.Logger _logger; public NLogger() { - var nlogConfigPathOption = ConfigurationManager.AppSettings["NLogConfigFilePath"]; + var nlogConfigPathOption = + ConfigurationHandler.ConfigurationHandler.GetConfiguration("NLogConfigFilePath"); if (nlogConfigPathOption == null) { throw new ArgumentNullException("nlogConfigPath"); diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs index cfbbd77..d39734c 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs @@ -1,11 +1,9 @@ using System; using System.Collections.Generic; -using System.Configuration; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; -using System.Security.Cryptography.X509Certificates; using Interfaces; using SQLite.Net; using SQLite.Net.Platform.Win32; @@ -368,7 +366,7 @@ namespace SQLiteRepository if (ident.UserId_FK!=-1) { //only check log gap if the card is associated to a user - var hysteresisThresholdMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["SwipeTimeGap"] ?? "3"); + var hysteresisThresholdMinutes = Convert.ToInt32(ConfigurationHandler.ConfigurationHandler.GetConfiguration("SwipeTimeGap") ?? "3"); var threshold = DateTime.UtcNow.AddMinutes(0 - hysteresisThresholdMinutes); var logs = _connection.Query( SQLiteProcedures.GET_LOGS_IN_LAST_X_MINUTES, diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj index 9efe2f3..131dd72 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.csproj @@ -69,6 +69,10 @@ + + {115250F6-F8C4-4F9B-A15F-251EA258D963} + ConfigurationHandler + {B7347B72-E208-423A-9D99-723B558EA3D7} Interfaces diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/ConfigMonitor.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/ConfigMonitor.cs deleted file mode 100644 index 026edea..0000000 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/ConfigMonitor.cs +++ /dev/null @@ -1,93 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace WindowsDataCenter -{ - public class ConfigMonitor - { - private readonly FileSystemWatcher _watcher; - private DateTime _lastChange; - - public ConfigMonitor(string configFilePath) - { - if (configFilePath == null) - { - throw new ArgumentNullException(nameof(configFilePath)); - } - - _lastChange = DateTime.MinValue; - configFilePath = string.Concat(System.Reflection.Assembly.GetEntryAssembly().Location, ".config"); - if (File.Exists(configFilePath)) - { - _watcher = new FileSystemWatcher(Path.GetDirectoryName(configFilePath), Path.GetFileName(configFilePath)) - { - EnableRaisingEvents = true - }; - _watcher.Changed += Watcher_Changed; - } - } - - ~ConfigMonitor() - { - _watcher.EnableRaisingEvents = false; - _watcher.Changed -= Watcher_Changed; - _watcher.Dispose(); - } - - public void Stop() - { - _watcher.EnableRaisingEvents = false; - _watcher.Changed -= Watcher_Changed; - _watcher.Dispose(); - } - - private void Watcher_Changed(object sender, FileSystemEventArgs e) - { - if ((DateTime.Now - _lastChange).Seconds <= 5 || IsFileLocked(e.FullPath)) return; - var monitoredSections = ConfigurationManager.AppSettings["monitoredSections"]; - - if (monitoredSections != null) - { - IEnumerable sections = monitoredSections.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); - if (sections.Any()) - { - foreach (var section in sections) - { - ConfigurationManager.RefreshSection(section); - } - } - } - else - { - Debug.WriteLine(ConfigurationManager.AppSettings["SwipeTimeGap"]); - ConfigurationManager.RefreshSection("AppSettings"); - Debug.WriteLine(ConfigurationManager.AppSettings["SwipeTimeGap"]); - } - _lastChange = DateTime.Now; - } - - private bool IsFileLocked(string filePath) - { - FileStream stream = null; - try - { - stream = File.OpenRead(filePath); - } - catch (IOException) - { - return true; - } - finally - { - stream?.Close(); - } - return false; - } - } -} diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Controllers/ApplicationController.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Controllers/ApplicationController.cs index e9835fd..8b3fc15 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Controllers/ApplicationController.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Controllers/ApplicationController.cs @@ -1,5 +1,4 @@ -using System.Configuration; -using System.Reflection; +using System.Reflection; using System.Web.Http; using Interfaces; @@ -18,7 +17,7 @@ namespace WindowsDataCenter DataBaseProvider = ninjectHelper.Get().GetType().ToString(), LoggerProvider = ninjectHelper.Get().GetType().ToString(), Version = Assembly.GetEntryAssembly().GetName().Version.ToString(), - ErrorEmailAddress = ConfigurationManager.AppSettings["BugSubmissionEmailAddress"] ?? "NONE" + ErrorEmailAddress = ConfigurationHandler.ConfigurationHandler.GetConfiguration("BugSubmissionEmailAddress") ?? "NONE" }; return Ok(appDetails); diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Controllers/UsersController.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Controllers/UsersController.cs index 0a65dee..b1cffd2 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Controllers/UsersController.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Controllers/UsersController.cs @@ -54,7 +54,7 @@ namespace WindowsDataCenter private int GetPageSize(int pageSize) { - var cfgPageSize = ConfigurationManager.AppSettings["DefaultPageSize"] ?? 10.ToString(); + var cfgPageSize = ConfigurationHandler.ConfigurationHandler.GetConfiguration("DefaultPageSize")?? 10.ToString(); return pageSize == -1 ? Convert.ToInt32(cfgPageSize) diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DataCenterService.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DataCenterService.cs index 230fec1..8acb365 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DataCenterService.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DataCenterService.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using System.ServiceProcess; using System.Threading; using Interfaces; @@ -18,7 +17,6 @@ namespace WindowsDataCenter private bool _stopMainWorkerThread; private Thread _mainWorkerThread; private ILogger _logger; - private ConfigMonitor _cfgWatcher; public void Start() { @@ -37,8 +35,7 @@ namespace WindowsDataCenter var ninjectInstance = NinjectHelper.GetInstance(); _logger = NinjectHelper.GetInstance().Get(); _logger.Trace("Starting Data Center Service"); - - _cfgWatcher = new ConfigMonitor(configPath); + _logger.Trace("Monitoring App.config for changes"); _mainWorkerThread = new Thread(MainWorkerThread) { @@ -62,10 +59,7 @@ namespace WindowsDataCenter _webApp.Dispose(); _logger.Trace("Stopped WebApi"); - - _cfgWatcher.Stop(); - _logger.Trace("Stopped Config Watcher"); - + _stopMainWorkerThread = true; if (_mainWorkerThread != null && _mainWorkerThread.IsAlive) { diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/StartOwin.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/StartOwin.cs index e9b05a2..2a2ee17 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/StartOwin.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/StartOwin.cs @@ -19,8 +19,6 @@ namespace WindowsDataCenter config.MapHttpAttributeRoutes(); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; - //ninject resolver for ApiController DI. - //config.DependencyResolver = new NinjectDependencyResolver(NinjectHelper.GetInstance().Kernel); //JSONP formatter for cross-domain requests. config.Formatters.Insert(0, new JsonpFormatter()); diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj index 65efa83..e632917 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj @@ -147,7 +147,6 @@ - @@ -262,6 +261,10 @@ + + {115250F6-F8C4-4F9B-A15F-251EA258D963} + ConfigurationHandler + {B7347B72-E208-423A-9D99-723B558EA3D7} Interfaces