-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
ConfigurationChangeTokenSource.cs
54 lines (47 loc) · 1.87 KB
/
ConfigurationChangeTokenSource.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
51
52
53
54
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
namespace Microsoft.Extensions.Options
{
/// <summary>
/// Creates <see cref="IChangeToken"/>s so that <see cref="IOptionsMonitor{TOptions}"/> gets
/// notified when <see cref="IConfiguration"/> changes.
/// </summary>
/// <typeparam name="TOptions"></typeparam>
public class ConfigurationChangeTokenSource<TOptions> : IOptionsChangeTokenSource<TOptions>
{
private readonly IConfiguration _config;
/// <summary>
/// Constructor taking the <see cref="IConfiguration"/> instance to watch.
/// </summary>
/// <param name="config">The configuration instance.</param>
public ConfigurationChangeTokenSource(IConfiguration config) : this(Options.DefaultName, config)
{
}
/// <summary>
/// Constructor taking the <see cref="IConfiguration"/> instance to watch.
/// </summary>
/// <param name="name">The name of the options instance being watched.</param>
/// <param name="config">The configuration instance.</param>
public ConfigurationChangeTokenSource(string? name, IConfiguration config)
{
ThrowHelper.ThrowIfNull(config);
_config = config;
Name = name ?? Options.DefaultName;
}
/// <summary>
/// The name of the option instance being changed.
/// </summary>
public string Name { get; }
/// <summary>
/// Returns the reloadToken from the <see cref="IConfiguration"/>.
/// </summary>
/// <returns></returns>
public IChangeToken GetChangeToken()
{
return _config.GetReloadToken();
}
}
}