1
|
using System;
|
2
|
using System.Collections.Generic;
|
3
|
using System.Text;
|
4
|
using System.Configuration;
|
5
|
using System.ComponentModel;
|
6
|
using System.Reflection;
|
7
|
using System.IO;
|
8
|
|
9
|
namespace AssemblySettings
|
10
|
{
|
11
|
/// <summary>
|
12
|
/// Provides access to the settings contained in custom configuration file or in the configuration file of
|
13
|
/// an assembly.
|
14
|
/// </summary>
|
15
|
public class ConfigurationFileApplicationSettings : IComponent, ISite, IServiceProvider
|
16
|
{
|
17
|
#region Fields
|
18
|
|
19
|
Provider _provider;
|
20
|
|
21
|
#endregion
|
22
|
|
23
|
#region Constructor
|
24
|
|
25
|
/// <summary>
|
26
|
/// Initializes a new instance of the <see cref="ConfigurationFileApplicationSettings"/> class.
|
27
|
/// </summary>
|
28
|
/// <param name="configuration">The configuration file.</param>
|
29
|
/// <param name="type">The type that contains the settings.</param>
|
30
|
/// <exception cref="ArgumentNullException"><paramref name="configuration"/> is null.</exception>
|
31
|
/// <exception cref="ArgumentNullException"><paramref name="type"/> is null.</exception>
|
32
|
public ConfigurationFileApplicationSettings(System.Configuration.Configuration configuration, Type type)
|
33
|
: this(configuration, type == null ? null : type.FullName)
|
34
|
{
|
35
|
}
|
36
|
|
37
|
/// <summary>
|
38
|
/// Initializes a new instance of the <see cref="ConfigurationFileApplicationSettings"/> class.
|
39
|
/// </summary>
|
40
|
/// <param name="configuration">The configuration file.</param>
|
41
|
/// <param name="sectionName">The name of the section containing the settings.</param>
|
42
|
/// <exception cref="ArgumentNullException"><paramref name="configuration"/> is null.</exception>
|
43
|
/// <exception cref="ArgumentNullException"><paramref name="sectionName"/> is null or empty.</exception>
|
44
|
public ConfigurationFileApplicationSettings(System.Configuration.Configuration configuration, string sectionName)
|
45
|
{
|
46
|
Init(configuration, sectionName);
|
47
|
}
|
48
|
|
49
|
/// <summary>
|
50
|
/// Initializes a new instance of the <see cref="ConfigurationFileApplicationSettings"/> class.
|
51
|
/// </summary>
|
52
|
/// <param name="assembly">The assembly to load the settings from.</param>
|
53
|
/// <param name="type">The type that contains the settings.</param>
|
54
|
/// <exception cref="ArgumentNullException"><paramref name="assembly"/> is null.</exception>
|
55
|
/// <exception cref="ArgumentNullException"><paramref name="type"/> is null.</exception>
|
56
|
public ConfigurationFileApplicationSettings(Assembly assembly, Type type)
|
57
|
: this(assembly, type == null ? null : type.FullName)
|
58
|
{
|
59
|
}
|
60
|
|
61
|
/// <summary>
|
62
|
/// Initializes a new instance of the <see cref="ConfigurationFileApplicationSettings"/> class.
|
63
|
/// </summary>
|
64
|
/// <param name="assembly">The assembly to load the settings from.</param>
|
65
|
/// <param name="sectionName">The name of the section containing the settings.</param>
|
66
|
/// <exception cref="ArgumentNullException"><paramref name="assembly"/> is null.</exception>
|
67
|
/// <exception cref="ArgumentNullException"><paramref name="sectionName"/> is null or empty.</exception>
|
68
|
/// <exception cref="ArgumentException">The assembly has been loaded from the GAC.</exception>
|
69
|
public ConfigurationFileApplicationSettings(Assembly assembly, string sectionName)
|
70
|
{
|
71
|
if (assembly == null)
|
72
|
{
|
73
|
throw new ArgumentNullException("assembly");
|
74
|
}
|
75
|
|
76
|
if (string.IsNullOrEmpty(sectionName))
|
77
|
{
|
78
|
throw new ArgumentNullException("sectionName");
|
79
|
}
|
80
|
|
81
|
if (assembly.GlobalAssemblyCache)
|
82
|
{
|
83
|
throw new ArgumentException("Assemblies loaded from the GAC are not supported.");
|
84
|
}
|
85
|
|
86
|
var path = new Uri(assembly.CodeBase).LocalPath;
|
87
|
|
88
|
System.Configuration.Configuration configuration;
|
89
|
|
90
|
if (string.IsNullOrEmpty(assembly.Location))
|
91
|
{
|
92
|
// The assembly has been loaded from memory so try to find its configuration file by its name
|
93
|
// In this case "CodeBase" points to the assembly that loaded the dynamic assembly
|
94
|
// so expect that the configuration file will be located in its directory.
|
95
|
|
96
|
// In this case the configuration file must be loaded by its full name because the
|
97
|
// "ConfigurationManager.OpenExeConfiguration" expects the specified executable file to exists.
|
98
|
|
99
|
var directoryName = Path.GetDirectoryName(path);
|
100
|
|
101
|
var extension = assembly.EntryPoint == null ? ".dll" : ".exe";
|
102
|
|
103
|
path = Path.Combine(directoryName, assembly.GetName().Name + extension + ".config");
|
104
|
|
105
|
configuration = ConfigurationManager.OpenMappedExeConfiguration(
|
106
|
new ExeConfigurationFileMap()
|
107
|
{
|
108
|
ExeConfigFilename = path,
|
109
|
},
|
110
|
ConfigurationUserLevel.None
|
111
|
);
|
112
|
}
|
113
|
else
|
114
|
{
|
115
|
configuration = ConfigurationManager.OpenExeConfiguration(path);
|
116
|
}
|
117
|
|
118
|
Init(configuration, sectionName);
|
119
|
}
|
120
|
|
121
|
/// <summary>
|
122
|
/// Initializes a new instance of the <see cref="ConfigurationFileApplicationSettings"/> class.
|
123
|
/// </summary>
|
124
|
/// <param name="configuration">The path to the configuration file.</param>
|
125
|
/// <param name="type">The type that contains the settings.</param>
|
126
|
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null or empty.</exception>
|
127
|
/// <exception cref="ArgumentNullException"><paramref name="type"/> is null.</exception>
|
128
|
public ConfigurationFileApplicationSettings(string path, Type type)
|
129
|
: this(path, type == null ? null : type.FullName)
|
130
|
{
|
131
|
}
|
132
|
|
133
|
/// <summary>
|
134
|
/// Initializes a new instance of the <see cref="ConfigurationFileApplicationSettings"/> class.
|
135
|
/// </summary>
|
136
|
/// <param name="configuration">The path to the configuration file.</param>
|
137
|
/// <param name="sectionName">The name of the section containing the settings.</param>
|
138
|
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null or empty.</exception>
|
139
|
/// <exception cref="ArgumentNullException"><paramref name="sectionName"/> is null or empty.</exception>
|
140
|
public ConfigurationFileApplicationSettings(string path, string sectionName)
|
141
|
{
|
142
|
if (string.IsNullOrEmpty(path))
|
143
|
{
|
144
|
throw new ArgumentNullException("path");
|
145
|
}
|
146
|
|
147
|
if (string.IsNullOrEmpty(sectionName))
|
148
|
{
|
149
|
throw new ArgumentNullException("sectionName");
|
150
|
}
|
151
|
|
152
|
// Convert the path to a full path to avoid looking for the file
|
153
|
// in the current working directory instead of the binary directory.
|
154
|
path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
|
155
|
|
156
|
var configuration = ConfigurationManager.OpenMappedExeConfiguration(
|
157
|
new ExeConfigurationFileMap()
|
158
|
{
|
159
|
ExeConfigFilename = path,
|
160
|
},
|
161
|
ConfigurationUserLevel.None
|
162
|
);
|
163
|
|
164
|
Init(configuration, sectionName);
|
165
|
}
|
166
|
|
167
|
/// <summary>
|
168
|
/// Inits the specified configuration.
|
169
|
/// </summary>
|
170
|
/// <param name="configuration">The configuration.</param>
|
171
|
/// <param name="sectionName">The name of the section containing the settings.</param>
|
172
|
/// <exception cref="ArgumentNullException"><paramref name="configuration"/> is <c>null</c>.</exception>
|
173
|
/// <param name="sectionName">The name of the section containing the settings.</param>
|
174
|
void Init(System.Configuration.Configuration configuration, string sectionName)
|
175
|
{
|
176
|
if (configuration == null)
|
177
|
{
|
178
|
throw new ArgumentNullException("configuration");
|
179
|
}
|
180
|
|
181
|
if (string.IsNullOrEmpty(sectionName))
|
182
|
{
|
183
|
throw new ArgumentNullException("sectionName");
|
184
|
}
|
185
|
|
186
|
_provider = new Provider(configuration, sectionName);
|
187
|
}
|
188
|
|
189
|
#endregion
|
190
|
|
191
|
#region IComponent Members
|
192
|
|
193
|
/// <summary>
|
194
|
/// Represents the method that handles the <see cref="E:System.ComponentModel.IComponent.Disposed"/> event of a component.
|
195
|
/// </summary>
|
196
|
public event EventHandler Disposed;
|
197
|
|
198
|
/// <summary>
|
199
|
/// Gets or sets the <see cref="T:System.ComponentModel.ISite"/> associated with the <see cref="T:System.ComponentModel.IComponent"/>.
|
200
|
/// </summary>
|
201
|
/// <value></value>
|
202
|
/// <returns>
|
203
|
/// The <see cref="T:System.ComponentModel.ISite"/> object associated with the component; or null, if the component does not have a site.
|
204
|
/// </returns>
|
205
|
ISite IComponent.Site
|
206
|
{
|
207
|
get
|
208
|
{
|
209
|
return this;
|
210
|
}
|
211
|
set
|
212
|
{
|
213
|
throw new NotSupportedException();
|
214
|
}
|
215
|
}
|
216
|
|
217
|
#endregion
|
218
|
|
219
|
#region ISite Members
|
220
|
|
221
|
/// <summary>
|
222
|
/// Gets the component associated with the <see cref="T:System.ComponentModel.ISite"/> when implemented by a class.
|
223
|
/// </summary>
|
224
|
/// <value></value>
|
225
|
/// <returns>
|
226
|
/// The <see cref="T:System.ComponentModel.IComponent"/> instance associated with the <see cref="T:System.ComponentModel.ISite"/>.
|
227
|
/// </returns>
|
228
|
IComponent ISite.Component
|
229
|
{
|
230
|
get
|
231
|
{
|
232
|
return this;
|
233
|
}
|
234
|
}
|
235
|
|
236
|
/// <summary>
|
237
|
/// Gets the <see cref="T:System.ComponentModel.IContainer"/> associated with the <see cref="T:System.ComponentModel.ISite"/> when implemented by a class.
|
238
|
/// </summary>
|
239
|
/// <value></value>
|
240
|
/// <returns>
|
241
|
/// The <see cref="T:System.ComponentModel.IContainer"/> instance associated with the <see cref="T:System.ComponentModel.ISite"/>.
|
242
|
/// </returns>
|
243
|
IContainer ISite.Container
|
244
|
{
|
245
|
get
|
246
|
{
|
247
|
return null;
|
248
|
}
|
249
|
}
|
250
|
|
251
|
/// <summary>
|
252
|
/// Determines whether the component is in design mode when implemented by a class.
|
253
|
/// </summary>
|
254
|
/// <value></value>
|
255
|
/// <returns>true if the component is in design mode; otherwise, false.
|
256
|
/// </returns>
|
257
|
bool ISite.DesignMode
|
258
|
{
|
259
|
get { return false; }
|
260
|
}
|
261
|
|
262
|
/// <summary>
|
263
|
/// Gets the friendly name used to refer to the provider during configuration.
|
264
|
/// </summary>
|
265
|
/// <value></value>
|
266
|
/// <returns>
|
267
|
/// The friendly name used to refer to the provider during configuration.
|
268
|
/// </returns>
|
269
|
string ISite.Name { get; set; }
|
270
|
|
271
|
#endregion
|
272
|
|
273
|
#region IServiceProvider Members
|
274
|
|
275
|
/// <summary>
|
276
|
/// Gets the service object of the specified type.
|
277
|
/// </summary>
|
278
|
/// <param name="serviceType">An object that specifies the type of service object to get.</param>
|
279
|
/// <returns>
|
280
|
/// A service object of type <paramref name="serviceType"/>.
|
281
|
/// -or-
|
282
|
/// null if there is no service object of type <paramref name="serviceType"/>.
|
283
|
/// </returns>
|
284
|
object IServiceProvider.GetService(Type serviceType)
|
285
|
{
|
286
|
if (serviceType == typeof(ISettingsProviderService))
|
287
|
{
|
288
|
return _provider;
|
289
|
}
|
290
|
else
|
291
|
{
|
292
|
return null;
|
293
|
}
|
294
|
}
|
295
|
|
296
|
#endregion
|
297
|
|
298
|
#region IDisposable Members
|
299
|
|
300
|
/// <summary>
|
301
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
302
|
/// </summary>
|
303
|
void IDisposable.Dispose()
|
304
|
{
|
305
|
var temp = Disposed;
|
306
|
if (temp != null)
|
307
|
{
|
308
|
temp(this, EventArgs.Empty);
|
309
|
}
|
310
|
}
|
311
|
|
312
|
#endregion
|
313
|
|
314
|
#region Provider class
|
315
|
|
316
|
class Provider : SettingsProvider, ISettingsProviderService
|
317
|
{
|
318
|
#region Properties
|
319
|
|
320
|
/// <summary>
|
321
|
/// Gets or sets the name of the currently running application.
|
322
|
/// </summary>
|
323
|
/// <value></value>
|
324
|
/// <returns>
|
325
|
/// A <see cref="T:System.String"/> that contains the application's shortened name, which does not contain a full path or extension, for example, SimpleAppSettings.
|
326
|
/// </returns>
|
327
|
public override string ApplicationName { get; set; }
|
328
|
|
329
|
#endregion
|
330
|
|
331
|
#region Fields
|
332
|
|
333
|
System.Configuration.Configuration _configuration;
|
334
|
|
335
|
ClientSettingsSection _applicationSection;
|
336
|
|
337
|
ClientSettingsSection _userSection;
|
338
|
|
339
|
string _sectionName;
|
340
|
|
341
|
#endregion
|
342
|
|
343
|
#region Constructor
|
344
|
|
345
|
/// <summary>
|
346
|
/// Inits the specified configuration.
|
347
|
/// </summary>
|
348
|
/// <param name="configuration">The configuration.</param>
|
349
|
/// <param name="sectionName">The name of the section containing the settings.</param>
|
350
|
/// <exception cref="ArgumentNullException"><paramref name="configuration"/> is <c>null</c>.</exception>
|
351
|
/// <param name="sectionName">The name of the section containing the settings.</param>
|
352
|
internal Provider(System.Configuration.Configuration configuration, string sectionName)
|
353
|
{
|
354
|
if (configuration == null)
|
355
|
{
|
356
|
throw new ArgumentNullException("configuration");
|
357
|
}
|
358
|
|
359
|
if (string.IsNullOrEmpty(sectionName))
|
360
|
{
|
361
|
throw new ArgumentNullException("sectionName");
|
362
|
}
|
363
|
|
364
|
_configuration = configuration;
|
365
|
|
366
|
_sectionName = sectionName;
|
367
|
|
368
|
var sectionGroup = configuration.GetSectionGroup("applicationSettings");
|
369
|
|
370
|
_applicationSection = sectionGroup == null ? null : sectionGroup.Sections[_sectionName] as ClientSettingsSection;
|
371
|
|
372
|
sectionGroup = configuration.GetSectionGroup("userSettings");
|
373
|
|
374
|
_userSection = sectionGroup == null ? null : sectionGroup.Sections[_sectionName] as ClientSettingsSection;
|
375
|
|
376
|
// The provider MUST have a name
|
377
|
Initialize(GetType().Name, null);
|
378
|
}
|
379
|
|
380
|
#endregion
|
381
|
|
382
|
#region Overriden methods
|
383
|
|
384
|
/// <summary>
|
385
|
/// Returns the collection of settings property values for the specified application instance and settings property group.
|
386
|
/// </summary>
|
387
|
/// <param name="context">A <see cref="T:System.Configuration.SettingsContext"/> describing the current application use.</param>
|
388
|
/// <param name="collection">A <see cref="T:System.Configuration.SettingsPropertyCollection"/> containing the settings property group whose values are to be retrieved.</param>
|
389
|
/// <returns>
|
390
|
/// A <see cref="T:System.Configuration.SettingsPropertyValueCollection"/> containing the values for the specified settings property group.
|
391
|
/// </returns>
|
392
|
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
|
393
|
{
|
394
|
SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
|
395
|
|
396
|
foreach (SettingsProperty property in collection)
|
397
|
{
|
398
|
var propertyValue = new SettingsPropertyValue(property);
|
399
|
|
400
|
var attribute = property.Attributes[typeof(SpecialSettingAttribute)] as SpecialSettingAttribute;
|
401
|
|
402
|
if (attribute != null && attribute.SpecialSetting == SpecialSetting.ConnectionString)
|
403
|
{
|
404
|
var connectionStrings = _configuration.ConnectionStrings.ConnectionStrings;
|
405
|
|
406
|
string connectionStringName = _sectionName + "." + property.Name;
|
407
|
|
408
|
if (connectionStrings != null && connectionStrings[connectionStringName] != null)
|
409
|
{
|
410
|
propertyValue.PropertyValue = connectionStrings[connectionStringName].ConnectionString;
|
411
|
}
|
412
|
else if (property.DefaultValue != null && property.DefaultValue is string)
|
413
|
{
|
414
|
propertyValue.PropertyValue = property.DefaultValue;
|
415
|
}
|
416
|
else
|
417
|
{
|
418
|
propertyValue.PropertyValue = string.Empty;
|
419
|
}
|
420
|
|
421
|
propertyValue.IsDirty = false;
|
422
|
|
423
|
values.Add(propertyValue);
|
424
|
|
425
|
continue;
|
426
|
}
|
427
|
|
428
|
var section = IsUserSetting(property) ? _userSection : _applicationSection;
|
429
|
|
430
|
SettingElement settingElement = null;
|
431
|
|
432
|
if (section != null)
|
433
|
{
|
434
|
foreach (SettingElement item in section.Settings)
|
435
|
{
|
436
|
if (item.Name == property.Name)
|
437
|
{
|
438
|
settingElement = item;
|
439
|
|
440
|
break;
|
441
|
}
|
442
|
}
|
443
|
}
|
444
|
|
445
|
if (settingElement != null)
|
446
|
{
|
447
|
string innerXml = settingElement.Value.ValueXml.InnerXml;
|
448
|
|
449
|
if (settingElement.SerializeAs == SettingsSerializeAs.String)
|
450
|
{
|
451
|
innerXml = settingElement.Value.ValueXml.InnerText;
|
452
|
}
|
453
|
|
454
|
propertyValue.SerializedValue = innerXml;
|
455
|
}
|
456
|
else if (property.DefaultValue != null)
|
457
|
{
|
458
|
propertyValue.SerializedValue = property.DefaultValue;
|
459
|
}
|
460
|
else
|
461
|
{
|
462
|
propertyValue.PropertyValue = null;
|
463
|
}
|
464
|
|
465
|
propertyValue.IsDirty = false;
|
466
|
|
467
|
values.Add(propertyValue);
|
468
|
}
|
469
|
|
470
|
return values;
|
471
|
}
|
472
|
|
473
|
/// <summary>
|
474
|
/// Sets the values of the specified group of property settings.
|
475
|
/// </summary>
|
476
|
/// <param name="context">A <see cref="T:System.Configuration.SettingsContext"/> describing the current application usage.</param>
|
477
|
/// <param name="collection">A <see cref="T:System.Configuration.SettingsPropertyValueCollection"/> representing the group of property settings to set.</param>
|
478
|
/// <exception cref="NotSupportedException">Always.</exception>
|
479
|
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
|
480
|
{
|
481
|
throw new NotSupportedException();
|
482
|
}
|
483
|
|
484
|
#endregion
|
485
|
|
486
|
#region ISettingsProviderService Members
|
487
|
|
488
|
/// <summary>
|
489
|
/// Returns the settings provider compatible with the specified settings property.
|
490
|
/// </summary>
|
491
|
/// <param name="property">The <see cref="T:System.Configuration.SettingsProperty"/> that requires serialization.</param>
|
492
|
/// <returns>
|
493
|
/// If found, the <see cref="T:System.Configuration.SettingsProvider"/> that can persist the specified settings property; otherwise, null.
|
494
|
/// </returns>
|
495
|
SettingsProvider ISettingsProviderService.GetSettingsProvider(SettingsProperty property)
|
496
|
{
|
497
|
return this;
|
498
|
}
|
499
|
|
500
|
#endregion
|
501
|
|
502
|
#region Private methods
|
503
|
|
504
|
/// <summary>
|
505
|
/// Determines whether the specified settings is in user scope.
|
506
|
/// </summary>
|
507
|
/// <param name="setting">The setting.</param>
|
508
|
/// <returns>
|
509
|
/// <c>true</c> if the specified settings is in user scope; otherwise, <c>false</c>.
|
510
|
/// </returns>
|
511
|
bool IsUserSetting(SettingsProperty setting)
|
512
|
{
|
513
|
bool userScope = setting.Attributes[typeof(UserScopedSettingAttribute)] is UserScopedSettingAttribute;
|
514
|
|
515
|
bool applicationScope = setting.Attributes[typeof(ApplicationScopedSettingAttribute)] is ApplicationScopedSettingAttribute;
|
516
|
|
517
|
if (userScope && applicationScope)
|
518
|
{
|
519
|
throw new ConfigurationErrorsException(string.Format("Both application and user scope is specified for setting '{0}'.", setting.Name));
|
520
|
}
|
521
|
|
522
|
if (!userScope && !applicationScope)
|
523
|
{
|
524
|
throw new ConfigurationErrorsException(string.Format("No scope is specified for setting '{0}'.", setting.Name));
|
525
|
}
|
526
|
|
527
|
return userScope;
|
528
|
}
|
529
|
|
530
|
#endregion
|
531
|
}
|
532
|
|
533
|
#endregion
|
534
|
}
|
535
|
}
|