try pulling ConfigMonitor into the same project to get it to work.

doesnt appear to work so parking it for now.
#54
This commit is contained in:
chris.watts90@outlook.com 2017-03-02 20:00:38 +00:00
parent a424260409
commit 44b8012b27
5 changed files with 104 additions and 12 deletions

View File

@ -81,7 +81,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ConfigMonitor\ConfigMonitor.csproj">
<Project>{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}</Project>
<Project>{6f3878b0-1e07-4de7-bfec-509ff4443b71}</Project>
<Name>ConfigMonitor</Name>
</ProjectReference>
<ProjectReference Include="..\Interfaces\Interfaces.csproj">

View File

@ -19,7 +19,7 @@ namespace ConfigMonitor
}
_lastChange = DateTime.MinValue;
//var configFile = string.Concat(System.Reflection.Assembly.GetEntryAssembly().Location, ".config");
configFilePath = string.Concat(System.Reflection.Assembly.GetEntryAssembly().Location, ".config");
if (File.Exists(configFilePath))
{
_watcher = new FileSystemWatcher(Path.GetDirectoryName(configFilePath), Path.GetFileName(configFilePath))
@ -34,7 +34,7 @@ namespace ConfigMonitor
{
_watcher.EnableRaisingEvents = false;
_watcher.Changed -= Watcher_Changed;
_watcher.Dispose();
_watcher.Dispose();
}
public void Stop()
@ -49,7 +49,7 @@ namespace ConfigMonitor
if ((DateTime.Now - _lastChange).Seconds <= 5 || IsFileLocked(e.FullPath)) return;
var monitoredSections = ConfigurationManager.AppSettings["monitoredSections"];
if (monitoredSections!= null)
if (monitoredSections != null)
{
IEnumerable<string> sections = monitoredSections.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (sections.Any())
@ -58,7 +58,7 @@ namespace ConfigMonitor
{
ConfigurationManager.RefreshSection(section);
}
}
}
}
else
{

View File

@ -0,0 +1,93 @@
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<string> 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;
}
}
}

View File

@ -18,7 +18,7 @@ namespace WindowsDataCenter
private bool _stopMainWorkerThread;
private Thread _mainWorkerThread;
private ILogger _logger;
private ConfigMonitor.ConfigMonitor _cfgWatcher;
private ConfigMonitor _cfgWatcher;
public void Start()
{
@ -38,7 +38,7 @@ namespace WindowsDataCenter
_logger = NinjectHelper.GetInstance().Get<ILogger>();
_logger.Trace("Starting Data Center Service");
_cfgWatcher = new ConfigMonitor.ConfigMonitor(configPath);
_cfgWatcher = new ConfigMonitor(configPath);
_logger.Trace("Monitoring App.config for changes");
_mainWorkerThread = new Thread(MainWorkerThread)
{

View File

@ -147,6 +147,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CardData.cs" />
<Compile Include="ConfigMonitor.cs" />
<Compile Include="Controllers\ApplicationController.cs" />
<Compile Include="Controllers\CardsController.cs" />
<Compile Include="Configuration.cs" />
@ -212,7 +213,9 @@
<Content Include="www\spa.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="App.config" />
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<Content Include="Help\DalSoft.WebApi.HelpPage.Views\api.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
@ -259,10 +262,6 @@
<Content Include="sqlite3.dll" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ConfigMonitor\ConfigMonitor.csproj">
<Project>{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}</Project>
<Name>ConfigMonitor</Name>
</ProjectReference>
<ProjectReference Include="..\Interfaces\Interfaces.csproj">
<Project>{B7347B72-E208-423A-9D99-723B558EA3D7}</Project>
<Name>Interfaces</Name>