create ConfigMonitor project to actively monitor the app.config file.

#54
This commit is contained in:
Chris.Watts90@outlook.com 2017-03-01 16:50:17 +00:00
parent 4ca281d826
commit 7501ca52fe
4 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
namespace ConfigMonitor
{
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;
//var configFile = 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
{
ConfigurationManager.RefreshSection("appSettings");
}
_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

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ConfigMonitor</RootNamespace>
<AssemblyName>ConfigMonitor</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConfigMonitor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ConfigMonitor")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ConfigMonitor")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("6f3878b0-1e07-4de7-bfec-509ff4443b71")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -28,6 +28,8 @@ Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "CardReaderServiceInstaller"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Installers", "Installers", "{10A7E78C-0D11-40DD-AEC3-27C2C507A926}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Installers", "Installers", "{10A7E78C-0D11-40DD-AEC3-27C2C507A926}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigMonitor", "ConfigMonitor\ConfigMonitor.csproj", "{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -104,6 +106,14 @@ Global
{119216DE-FC7F-408A-9C2C-105874449E42}.Release|Any CPU.ActiveCfg = Release|x86 {119216DE-FC7F-408A-9C2C-105874449E42}.Release|Any CPU.ActiveCfg = Release|x86
{119216DE-FC7F-408A-9C2C-105874449E42}.Release|x86.ActiveCfg = Release|x86 {119216DE-FC7F-408A-9C2C-105874449E42}.Release|x86.ActiveCfg = Release|x86
{119216DE-FC7F-408A-9C2C-105874449E42}.Release|x86.Build.0 = Release|x86 {119216DE-FC7F-408A-9C2C-105874449E42}.Release|x86.Build.0 = Release|x86
{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Debug|x86.ActiveCfg = Debug|Any CPU
{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Debug|x86.Build.0 = Debug|Any CPU
{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Release|Any CPU.Build.0 = Release|Any CPU
{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Release|x86.ActiveCfg = Release|Any CPU
{6F3878B0-1E07-4DE7-BFEC-509FF4443B71}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE