Skip to content

Commit b255e5f

Browse files
#38 Update JsonConfig<TConfig>.Current to consider AsyncLocal mode of AtataContext
1 parent 207124e commit b255e5f

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/Atata.Configuration.Json/JsonConfig`1.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public abstract class JsonConfig<TConfig> : JsonSection
1515
private static TConfig currentThreadStaticConfig;
1616

1717
private static TConfig currentStaticConfig;
18+
19+
#if NET46 || NETSTANDARD2_0
20+
private static System.Threading.AsyncLocal<TConfig> currentAsyncLocalConfig = new System.Threading.AsyncLocal<TConfig>();
21+
#endif
1822
#pragma warning restore S2743 // Static fields should not be used in generic types
1923

2024
/// <summary>
@@ -24,14 +28,29 @@ public abstract class JsonConfig<TConfig> : JsonSection
2428

2529
/// <summary>
2630
/// Gets or sets the current <see cref="JsonConfig{TConfig}"/> instance.
31+
/// Keeps in sync with <see cref="AtataContext.Current"/> relying on its <see cref="AtataContext.ModeOfCurrent"/> value.
2732
/// </summary>
2833
public static TConfig Current
2934
{
30-
get => AtataContext.IsThreadStatic ? currentThreadStaticConfig : currentStaticConfig;
35+
get
36+
{
37+
return AtataContext.ModeOfCurrent == AtataContextModeOfCurrent.ThreadStatic
38+
? currentThreadStaticConfig
39+
#if NET46 || NETSTANDARD2_0
40+
: AtataContext.ModeOfCurrent == AtataContextModeOfCurrent.AsyncLocal
41+
? currentAsyncLocalConfig.Value
42+
#endif
43+
: currentStaticConfig;
44+
}
45+
3146
set
3247
{
33-
if (AtataContext.IsThreadStatic)
48+
if (AtataContext.ModeOfCurrent == AtataContextModeOfCurrent.ThreadStatic)
3449
currentThreadStaticConfig = value;
50+
#if NET46 || NETSTANDARD2_0
51+
else if (AtataContext.ModeOfCurrent == AtataContextModeOfCurrent.AsyncLocal)
52+
currentAsyncLocalConfig.Value = value;
53+
#endif
3554
else
3655
currentStaticConfig = value;
3756
}

0 commit comments

Comments
 (0)