Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public static class AsyncLocalExample
public static async Task DoWork()
{
AsyncLocalInt.Value = 1;
Console.WriteLine($"AsyncLocal value in DoMainWork method: {AsyncLocalInt.Value}");
Console.WriteLine($"AsyncLocal value in DoWork method: {AsyncLocalInt.Value}");

await DoSubTaskLevel1();

Console.WriteLine($"AsyncLocal value in DoMainWork method after executing " +
Console.WriteLine($"AsyncLocal value in DoWork method after executing " +
$"DoSubTaskLevel1 method: {AsyncLocalInt.Value}");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace PersistValuesInAsyncFlowUsingAsyncLocalInCSharp;

public static class AsyncLocalReferenceTypeExample
{
public static readonly AsyncLocal<List<string>> AsyncLocalList = new();

public static async Task DoWork()
{
AsyncLocalList.Value = ["Added in DoWork"];

await DoSubTask();

Console.WriteLine($"AsyncLocal list in DoWork method after executing DoSubTask method: " +
$"{string.Join(", ", AsyncLocalList.Value)}");
}

private static async Task DoSubTask()
{
AsyncLocalList.Value!.Add("Added in DoSubTask");

await Task.Delay(100);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace PersistValuesInAsyncFlowUsingAsyncLocalInCSharp;

public static class AsyncLocalVsThreadLocalExample
{
private static readonly AsyncLocal<string> AsyncLocalString = new();
private static readonly ThreadLocal<string> ThreadLocalString = new();

public static async Task DoWork()
{
AsyncLocalString.Value = "Set in DoWork";
ThreadLocalString.Value = "Set in DoWork";

var thread = new Thread(() =>
Console.WriteLine($"New thread, AsyncLocal: {AsyncLocalString.Value}, " +
$"ThreadLocal: {ThreadLocalString.Value ?? "null"}"));
thread.Start();
thread.Join();

Task task;
using (ExecutionContext.SuppressFlow())
{
task = Task.Run(() =>
Console.WriteLine($"Task.Run with suppressed flow, AsyncLocal: {AsyncLocalString.Value ?? "null"}"));
}

await task;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@

Console.WriteLine();

Console.WriteLine("AsyncLocalVsThreadLocalExample execution: ");
await AsyncLocalVsThreadLocalExample.DoWork();

Console.WriteLine();

Console.WriteLine("AsyncLocalReferenceTypeExample execution: ");
await AsyncLocalReferenceTypeExample.DoWork();

Console.WriteLine();

Console.WriteLine("AsyncLocalNotifyExample execution: ");
await AsyncLocalNotifyExample.DoWork();
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Tests;
public class AsyncLocalExampleTests
{
[TestMethod]
public async Task GivenAsyncLocalExample_WhenDoWork_AsyncValueIsOne()
public async Task GivenAsyncLocalExample_WhenDoWork_ThenCallerKeepsDefaultValue()
{
await AsyncLocalExample.DoWork();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class AsyncLocalNotifyExampleTests
{
[TestMethod]
public async Task GivenAsyncLocalNotifyExample_WhenDoWork_AsyncValueIsOne()
public async Task GivenAsyncLocalNotifyExample_WhenDoWork_ThenCallerKeepsNullValue()
{
await AsyncLocalNotifyExample.DoWork();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Tests;

using PersistValuesInAsyncFlowUsingAsyncLocalInCSharp;

[TestClass]
public class AsyncLocalReferenceTypeExampleTests
{
[TestMethod]
public async Task GivenAsyncLocalReferenceTypeExample_WhenDoWork_ThenCallerSeesChildChangeToList()
{
var output = new StringWriter();
var originalOutput = Console.Out;
Console.SetOut(output);

try
{
await AsyncLocalReferenceTypeExample.DoWork();
}
finally
{
Console.SetOut(originalOutput);
}

StringAssert.Contains(output.ToString(), "Added in DoWork, Added in DoSubTask");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Tests;

using PersistValuesInAsyncFlowUsingAsyncLocalInCSharp;

[TestClass]
public class AsyncLocalVsThreadLocalExampleTests
{
[TestMethod]
public async Task GivenAsyncLocalVsThreadLocalExample_WhenDoWork_ThenOnlyAsyncLocalFlowsUntilSuppressed()
{
var output = new StringWriter();
var originalOutput = Console.Out;
Console.SetOut(output);

try
{
await AsyncLocalVsThreadLocalExample.DoWork();
}
finally
{
Console.SetOut(originalOutput);
}

StringAssert.Contains(output.ToString(), "New thread, AsyncLocal: Set in DoWork, ThreadLocal: null");
StringAssert.Contains(output.ToString(), "Task.Run with suppressed flow, AsyncLocal: null");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,10 +10,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading