如何在同一解决方案中调用另一个项目中的会话变量 Asp .NET Core 3

How to call Session variables in another project in the Same Solution Asp .NET Core 3

我的系统中有运行个项目,如下图:

  1. 启动项目是项目1
  2. BLL 项目(在这个项目中我正在设置会话)
  3. Common Project(在这个项目中只有我想调用session)

在此class中,我无法创建构造函数。这是我的 class:

namespace Project.Common
{
   public class Share
   {
        public const string SESSION_CURRENT_COMPANY = "Company";

        public static int LoggedInMembersCompanyID
        {
            get
            {    
                    var companyIdSession = HttpContext.Session.GetInt32(Common.SESSION_CURRENT_COMPANY);
                    int _companyIdSessionID = 0;
                    if (companyIdSession != null)
                    {
                        int.TryParse(companyIdSession.ToString(), out _companyIdSessionID);
                    }
                    return _companyIdSessionID;
            }
        }
    }
}
  1. 您必须在 startup.cs 配置函数中添加此 app.UsesEndpoits

    Common.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>(), Configuration);

  2. 转到公共项目并转到 Common.cs 在顶部添加以下内容:

private static IHttpContextAccessor _httpContextAccessor; 
private static IConfiguration _configuration;
public static void Configure(IHttpContextAccessor httpContextAccessor,IConfiguration configuration)
{
                _httpContextAccessor = httpContextAccessor;
                _configuration = configuration;
}
  1. 获取Session值如下图:
public static int LoggedInMembersCompanyID
{
  get
     { 
       var companyIdSession = HttpContext.Current.Session[Share.SESSION_CURRENT_COMPANY];
       int _companyIdSessionID = 0;
       if (companyIdSession != null)
       {
         int.TryParse(companyIdSession.ToString(), out _companyIdSessionID);
       }
        return _companyIdSessionID; 
    }
}