c# - 带有 AsyncLocal 与 Scope 服务的单例

标签 c# asp.net-core .net-core httpcontext

我查看了如何在 .NET Core 中创建 HttpContext。然后我发现有一个类叫HttpContextFactory创建和分配 HttpContext对象变成 HttpContext HttpContextAccessor 的属性(property)类(class)。为了在我们的代码中使用 HttpContext 对象,我们将 IHttpContextAccessor 注入(inject)到需要该对象的类的构造函数中。

当我查看 HttpContextAccessor 的实现时,显然它的 HttpContext 属性从私有(private)的 AsyncLocal 中获取了 HttpContext 对象值。变量和后来在 HttpContextAccessor 上注册为 单例 .

https://github.com/aspnet/AspNetCore/blob/master/src/Http/Http/src/HttpContextAccessor.cs

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading;

namespace Microsoft.AspNetCore.Http
{
    public class HttpContextAccessor : IHttpContextAccessor
    {
        private static AsyncLocal<HttpContextHolder> _httpContextCurrent = new AsyncLocal<HttpContextHolder>();

        public HttpContext HttpContext
        {
            get
            {
                return  _httpContextCurrent.Value?.Context;
            }
            set
            {
                var holder = _httpContextCurrent.Value;
                if (holder != null)
                {
                    // Clear current HttpContext trapped in the AsyncLocals, as its done.
                    holder.Context = null;
                }

                if (value != null)
                {
                    // Use an object indirection to hold the HttpContext in the AsyncLocal,
                    // so it can be cleared in all ExecutionContexts when its cleared.
                    _httpContextCurrent.Value = new HttpContextHolder { Context = value };
                }
            }
        }

        private class HttpContextHolder
        {
            public HttpContext Context;
        }
    }
}

我很好奇,这样做而不是使用 有什么好处?范围 服务?在我看来,两者都会使对象在请求范围内可用。

如果它是一个范围服务,我认为 HttpContextAccessor 看起来像这样
using System.Threading;

namespace Microsoft.AspNetCore.Http
{
    public class HttpContextAccessor : IHttpContextAccessor
    {
        private HttpContextHolder _httpContextCurrent;

        public HttpContext HttpContext
        {
            get
            {
                return  _httpContextCurrent?.Context;
            }
            set
            {
                if (value != null)
                {
                    _httpContextCurrent = new HttpContextHolder { Context = value };
                }
            }
        }

        private class HttpContextHolder
        {
            public HttpContext Context;
        }
    }
}

然后将其用作范围服务
services.TryAddScope<IHttpContextAccessor, HttpContextAccessor>();

我想知道每种方法的优缺点是什么,以便我了解在为我的项目创建库时何时将 Singleton 与 AsyncLocal 或 Scope 一起使用。

最佳答案

只要是单例,解析IHttpContextAccessor实例可以由单例服务永久持有并正常工作,而如果单例服务解析范围IHttpContextAccessor,则可能会导致问题。 .

关于c# - 带有 AsyncLocal 与 Scope 服务的单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56762651/

相关文章:

c# - 将 LINQ intersect 函数与非基本类型结合使用

azure - Web App Bot 和 Azure Bot 有什么区别

c# - JsonStringEnumConverter (System.Text.Json) 是否支持 null 值?

c# - 限制 Web 请求的 CPU 使用率

c# - 自动调整 TabItem 宽度以填充空格?

c# - EF 代码第一次迁移 : recreate database and Seed

asp.net - ADO.NET 实体数据模型缺失 Visual Studio 2015 社区

angular - spa.UseAngularCliServer() 中间件如何为网页提供服务?

c# - 没有注册类型 'Microsoft.Extensions.Logging.ILoggingBuilder' 的服务

odbc - 如何在我的项目中为 dotnet 核心引用 System.Data.Odbc?