AsyncLocal<T> 类

定义

表示对于给定异步控制流(如异步方法)是本地数据的环境数据。

generic <typename T>
public ref class AsyncLocal sealed
public sealed class AsyncLocal<T>
type AsyncLocal<'T> = class
Public NotInheritable Class AsyncLocal(Of T)

类型参数

T

环境数据的类型。

继承
AsyncLocal<T>

示例

以下示例使用 AsyncLocal<T> 类在异步流中保留字符串值。 它还将 的用法 AsyncLocal<T> 与 进行 ThreadLocal<T>对比。

using System;
using System.Threading;
using System.Threading.Tasks;

class Example
{
    static AsyncLocal<string> _asyncLocalString = new AsyncLocal<string>();

    static ThreadLocal<string> _threadLocalString = new ThreadLocal<string>();

    static async Task AsyncMethodA()
    {
        // Start multiple async method calls, with different AsyncLocal values.
        // We also set ThreadLocal values, to demonstrate how the two mechanisms differ.
        _asyncLocalString.Value = "Value 1";
        _threadLocalString.Value = "Value 1";
        var t1 = AsyncMethodB("Value 1");

        _asyncLocalString.Value = "Value 2";
        _threadLocalString.Value = "Value 2";
        var t2 = AsyncMethodB("Value 2");

        // Await both calls
        await t1;
        await t2;
     }

    static async Task AsyncMethodB(string expectedValue)
    {
        Console.WriteLine("Entering AsyncMethodB.");
        Console.WriteLine("   Expected '{0}', AsyncLocal value is '{1}', ThreadLocal value is '{2}'", 
                          expectedValue, _asyncLocalString.Value, _threadLocalString.Value);
        await Task.Delay(100);
        Console.WriteLine("Exiting AsyncMethodB.");
        Console.WriteLine("   Expected '{0}', got '{1}', ThreadLocal value is '{2}'", 
                          expectedValue, _asyncLocalString.Value, _threadLocalString.Value);
    }

    static async Task Main(string[] args)
    {
        await AsyncMethodA();
    }
}
// The example displays the following output:
//   Entering AsyncMethodB.
//      Expected 'Value 1', AsyncLocal value is 'Value 1', ThreadLocal value is 'Value 1'
//   Entering AsyncMethodB.
//      Expected 'Value 2', AsyncLocal value is 'Value 2', ThreadLocal value is 'Value 2'
//   Exiting AsyncMethodB.
//      Expected 'Value 2', got 'Value 2', ThreadLocal value is ''
//   Exiting AsyncMethodB.
//      Expected 'Value 1', got 'Value 1', ThreadLocal value is ''
Imports System.Threading
Imports System.Threading.Tasks

Module Example
    Dim _asyncLocalString As New AsyncLocal(Of String)()

    Dim _threadLocalString As New ThreadLocal(Of String)()

    Async Function AsyncMethodA() As Task
        ' Start multiple async method calls, with different AsyncLocal values.
        ' We also set ThreadLocal values, to demonstrate how the two mechanisms differ.
        _asyncLocalString.Value = "Value 1"
        _threadLocalString.Value = "Value 1"
        Dim t1 = AsyncMethodB("Value 1")

        _asyncLocalString.Value = "Value 2"
        _threadLocalString.Value = "Value 2"
        Dim t2 = AsyncMethodB("Value 2")

        ' Await both calls
        await t1
        await t2
     End Function

    Async Function AsyncMethodB(expectedValue As String) As Task
        Console.WriteLine("Entering AsyncMethodB.")
        Console.WriteLine("   Expected '{0}', AsyncLocal value is '{1}', ThreadLocal value is '{2}'", 
                          expectedValue, _asyncLocalString.Value, _threadLocalString.Value)
        await Task.Delay(100)
        Console.WriteLine("Exiting AsyncMethodB.")
        Console.WriteLine("   Expected '{0}', got '{1}', ThreadLocal value is '{2}'", 
                          expectedValue, _asyncLocalString.Value, _threadLocalString.Value)
    End Function

   Public Sub Main()
       AsyncMethodA.Wait()
   End Sub
End Module
' The example displays the following output:
'   Entering AsyncMethodB.
'      Expected 'Value 1', AsyncLocal value is 'Value 1', ThreadLocal value is 'Value 1'
'   Entering AsyncMethodB.
'      Expected 'Value 2', AsyncLocal value is 'Value 2', ThreadLocal value is 'Value 2'
'   Exiting AsyncMethodB.
'      Expected 'Value 2', got 'Value 2', ThreadLocal value is ''
'   Exiting AsyncMethodB.
'      Expected 'Value 1', got 'Value 1', ThreadLocal value is ''

注解

由于基于任务的异步编程模型倾向于抽象化线程的使用, AsyncLocal<T> 因此实例可用于跨线程持久保存数据。

当与当前线程关联的值发生更改时,类 AsyncLocal<T> 还提供可选通知,可能是因为通过设置 Value 属性显式更改,或者在线程遇到 await 或其他上下文转换时隐式更改。

构造函数

AsyncLocal<T>()

实例化不接收更改通知的 AsyncLocal<T> 实例。

AsyncLocal<T>(Action<AsyncLocalValueChangedArgs<T>>)

实例化接收更改通知的 AsyncLocal<T> 本地实例。

属性

Value

获取或设置环境数据的值。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于