-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathExeConfiguration.cs
50 lines (42 loc) · 2.41 KB
/
ExeConfiguration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
namespace ServiceControl.Configuration
{
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
public static class ExeConfiguration
{
// ConfigurationManager on .NET is looking for {assembly}.dll.config files, but all previous versions of ServiceControl will have {assembly}.exe.config instead.
// This code reads in the exe.config files and adds all the values into the ConfigurationManager's collections.
public static void PopulateAppSettings(Assembly assembly)
{
var location = Path.GetDirectoryName(assembly.Location);
var assemblyName = Path.GetFileNameWithoutExtension(assembly.Location);
var exeConfigPath = Path.Combine(location, $"{assemblyName}.exe.config");
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = exeConfigPath };
var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
foreach (var key in configuration.AppSettings.Settings.AllKeys)
{
ConfigurationManager.AppSettings.Set(key, configuration.AppSettings.Settings[key].Value);
}
// The connection strings collection has had its read only flag set, so we need to clear it before we can add items to it
UnsetCollectionReadonly(ConfigurationManager.ConnectionStrings);
foreach (var connectionStringSetting in configuration.ConnectionStrings.ConnectionStrings.Cast<ConnectionStringSettings>())
{
ConfigurationManager.ConnectionStrings.Add(connectionStringSetting);
}
// Put the collection back into its previous state after we're done adding items to it
SetCollectionReadOnly(ConfigurationManager.ConnectionStrings);
}
static void UnsetCollectionReadonly(ConfigurationElementCollection collection)
{
ref bool field = ref GetReadOnlyFieldRef(collection);
field = false;
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_readOnly")]
static extern ref bool GetReadOnlyFieldRef(ConfigurationElementCollection collection);
}
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "SetReadOnly")]
static extern void SetCollectionReadOnly(ConfigurationElementCollection collection);
}
}