Shared/Kernelx.AspNetCore/Kernelx/Contexts/NetCoreContextWorkload.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace Kernelx.Contexts
{
    /// <summary>
    /// 
    /// </summary>
    public class NetCoreContextWorkload : DefaultContextWorkload, IContextWorkload
    {

        private HttpContext _httpContext;
        private IList<IContextScope> _contextScopes;

        private readonly Lazy<HttpRequestParameter> _rqLazy;

        public NetCoreContextWorkload(IServiceProvider root, HttpContext httpContext, Func<ICurrentUser> getCurrentUser)
            : base(getCurrentUser, new DefaultCurrentUser("-1", "unknown", false), type => root.GetService(type), null)
        {
            _httpContext = httpContext;
            _contextScopes = new List<IContextScope>();
            _rqLazy = new Lazy<HttpRequestParameter>(() => new HttpRequestParameter(_httpContext.Request));
        }

        public override IContextScope CreateServiceScope()
        {
            var sf = base.GetService(typeof(IServiceScopeFactory)) as IServiceScopeFactory;
            var result = new NetCoreContextScope(()=>User,sf.CreateScope(), scope => _contextScopes.Remove(scope));
            _contextScopes.Add(result);
            return result;
        }

        public override object GetService(Type serviceType)
        {
            if (serviceType == typeof(IRequestParameter))
                return _rqLazy.Value;

            if (serviceType == typeof(HttpContext))
                return _httpContext;

            return _contextScopes.Count == 0 ? _httpContext.RequestServices.GetService(serviceType)
                : _contextScopes.Last().GetService(serviceType);
        } 
    }

    public class NetCoreContextScope : IContextScope
    {
        private IServiceScope _serviceScope;
        private Action<IContextScope> _remove;
        private Func<ICurrentUser> _getUser;
        public NetCoreContextScope(Func<ICurrentUser> getUser,IServiceScope scope, Action<IContextScope> remove)
        {
            _getUser= getUser;
            _serviceScope = scope;
            _remove = remove;
        }
后面还有 19 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。