.net core 中使用AsyncLocal传递变量

官网

https://github.com/dotnet/runtime/blob/16b6369b7509e58c35431f05681a9f9e5d10afaa/src/libraries/System.Private.CoreLib/src/System/Threading/AsyncLocal.cs#L45

AsyncLocal是一个在.NET中用来在同步任务和异步任务中保持全局变量的工具类。它允许你在不同线程的同一个对象中保留一个特定值,这样你可以在不同的函数和任务中访问这个值。这是在实现异步任务中维持一致性和优雅性的一种重要手段。

中间件中使用

csharp 复制代码
 public class BaseClass
 {
     public string Id { get; set; }
 }

    public static class AmbientContext
    {
        public static readonly ConcurrentDictionary<string, AsyncLocal<object?>>
            _contexts = new(StringComparer.Ordinal);

        public static void Set<T>(string key, [MaybeNull] T val)
        {
            AsyncLocal<object?> keyctx = _contexts.AddOrUpdate(
                    key,
                    k => new AsyncLocal<object?>(),
                    (k, al) => al);
            keyctx.Value = (object?)val;
        }

        [return: MaybeNull]
        public static T Get<T>(string key)
        {
            return _contexts.TryGetValue(key, out AsyncLocal<object?>? keyctx)
                     ? (T)(keyctx!.Value ?? default(T)!)
                     : default(T);
        }
    }


    public class AmbientContextMiddleware
    {
        private readonly RequestDelegate _next;

        public AmbientContextMiddleware(RequestDelegate next) =>
            _next = next;

        public async Task Invoke(HttpContext context)
        {
            string corrId =
                context.Request
                       .Headers["x-foocorp-correlationId"]
                       .FirstOrDefault(Guid.NewGuid().ToString());

            context.Request.Headers.Add("x-foocorp-correlationId", corrId);

            AmbientContext.Set<BaseClass>(corrId, new BaseClass() { Id=DateTime.Now.ToString()});

            await _next.Invoke(context);

            

            // TODO: emit corrid response header, esp if _we_ created it
        }
    }

[HttpGet]
public async Task<DatasetSmallMovie> GetByID(string id,string indexName)
{
    Console.WriteLine(DateTime.Now.ToString());
    Request.Headers.TryGetValue("x-foocorp-correlationId", out var headersvalue);

    Console.WriteLine(headersvalue);
    await Task.Delay(2000);

    Console.WriteLine(AmbientContext.Get<BaseClass>(headersvalue).Id);

    return await _meilisearchHelper.GetByID<DatasetSmallMovie>(id, indexName);
}

普通使用

csharp 复制代码
    using System;
    using System.Threading;
    using System.Threading.Tasks;
    public class LogContext { public string StackTrace { get; set; } public string UserInfo { get; set; } }
    public class TenantContext { public string Name { get; set; } }
    public class Program
    {
        private static AsyncLocal<LogContext> _logContext = new AsyncLocal<LogContext>(); private static AsyncLocal<TenantContext> _tenantContext = new AsyncLocal<TenantContext>();
        public static async Task Main(string[] args)
        {
            _logContext.Value = new LogContext { StackTrace = "Main Stack Trace", UserInfo = "User1" }; _tenantContext.Value = new TenantContext { Name = "Tenant A" };
            Console.WriteLine($"Initial Log Context: {_logContext.Value.StackTrace}, User: {_logContext.Value.UserInfo}, Tenant: {_tenantContext.Value.Name}");
            await Task.Run(() => LogAndProcess(new LogContext { StackTrace = "Child Stack Trace", UserInfo = "User2" }, new TenantContext { Name = "Tenant B" }));
            Console.WriteLine($"After Task Log Context: {_logContext.Value.StackTrace}, User: {_logContext.Value.UserInfo}, Tenant: {_tenantContext.Value.Name}");
        }
        private static void LogAndProcess(LogContext logContext, TenantContext tenant)
        {
            _logContext.Value = logContext; _tenantContext.Value = tenant;
            Console.WriteLine($"In Task Log Context: {_logContext.Value.StackTrace}, User: {_logContext.Value.UserInfo}, Tenant: {_tenantContext.Value.Name}");
            // Simulate some processing        Task.Delay(1000).Wait();
            Console.WriteLine($"After Processing Log Context: {_logContext.Value.StackTrace}, User: {_logContext.Value.UserInfo}, Tenant: {_tenantContext.Value.Name}");
        }
    }
相关推荐
鸠摩智首席音效师3 天前
如何在 macOS 上安装 .NET Core ?
macos·.netcore
宝桥南山7 天前
Microsoft Agent Framework(MAF) - 如何将workflow或者A2A client转换成一个AI Agent
microsoft·ai·微软·aigc·.net·.netcore
滴滴答答哒16 天前
.NET Core 基于 AOP + Polly 实现数据库死锁自动重试
数据库·.netcore·sqlsugar
.NET修仙日记20 天前
.NET EFCore批量插入性能优化实战:30秒 → 0.5秒
性能优化·c#·.net·.netcore·微软技术·efcore·踩坑实录
Kimhill张23 天前
.net core8 WPF 依赖注入(DI)
wpf·.netcore
wangl_9224 天前
C# / .NET 在工业环境中的优势
开发语言·c#·.net·.netcore·.net core·visual studio
豆豆1 个月前
信创环境下CMS国产化适配实践:以.NET Core路线为例的技术验证
.netcore·cms·信创·国产化·建站系统·内容管理系统·网站管理系统
时光追逐者1 个月前
C#/.NET/.NET Core技术前沿周刊 | 第 70 期(2026年5.01-5.10)
c#·.net·.netcore
van久1 个月前
Day20:AutoMapper 对象映射
.netcore
van久1 个月前
Day23 登录 + 颁发 Token(DDD 四层架构 + 企业标准)
.netcore