HttpContext.Current.Session访问的奇怪行为

时间:2020-04-28 16:11:32

标签: session httpcontext

在WCF服务的 SaveOrder 方法中,我正在调用一个名为 HandleSaveOrderPostProcessing 的方法,而忘记了异步方法,我在线程池线程中添加了该方法,同时传递了HttpContext.Current。 &OperationContext.Current在此方法中如下

    public ServiceResult SaveOrder(Models.Orderhead order)
    {
     var result = new ServiceResult();
     ........
     ........
        var httpcontext = System.Web.HttpContext.Current;
            var opcontext = System.ServiceModel.OperationContext.Current;

            //Task t = Task.Factory.StartNew(() => HandleSaveOrderPostProcessing(order, result, httpcontext, opcontext));
            Task t = Task.Run(async() => await HandleSaveOrderPostProcessing(order, result, httpcontext, opcontext)).ContinueWith((tsk) =>
            {
                var iexs = tsk.Exception.InnerExceptions;
                StringBuilder exMsg = new StringBuilder(tsk.Exception.Message);
                foreach(var iex in iexs)
                {
                    exMsg.AppendLine(iex.Message);
                }

                return HandleError(order, result, "An unhandled error occurred processing the order.", exMsg.ToString(), ServiceResult.Status.Error);

            }, TaskContinuationOptions.OnlyOnFaulted);

     return result; 
    }

    private async Task HandleSaveOrderPostProcessing(Models.Orderhead order, ServiceResult result, System.Web.HttpContext httpcontext, System.ServiceModel.OperationContext opcontext)
    {
            System.Web.HttpContext.Current = httpcontext;
            System.ServiceModel.OperationContext.Current = opcontext;

            var issCredentialsCached = AuthCache.IsCredentialsCached;//Throwing error sometimes

            .............
            .............
            .............
            var issCredentialsCached = AuthCache.IsCredentialsCached;//working exactly
            .............
            .............
    }

    public class AuthCache
    {
      public static bool IsCredentialsCached
      {
        get
        {
          try
          {
             *bool* isCached1 = (HttpContext.Current != null && HttpContext.Current.User != null && HttpContext.Current.Cache != null && HttpContext.Current.Cache.Count > 0 &&
        (HttpContext.Current.Session == null ||
        (
          HttpContext.Current.Session != null && (
          HttpContext.Current.Session[Globals.SessionNameCompanyUser] == null &&
          HttpContext.Current.Session[Globals.SessionNameCompany] == null &&
          HttpContext.Current.Session[Globals.SessionNameUser] == null)
        )));

      return isCached1;
    }
    catch(Exception ex1)
    {
      var builder = new StringBuilder();
      try
      {
        if (HttpContext.Current != null)
        {
          builder.AppendLine("HttpContext.Current is not null.");
        }
        if (HttpContext.Current.User != null)
        {
          builder.AppendLine("HttpContext.Current.User is not null.");
        }
        if (HttpContext.Current.Cache != null)
        {
          builder.AppendLine("HttpContext.Current.Cache is not null.");
        }
        if (HttpContext.Current.Cache.Count > 0)
        {
          builder.AppendLine("HttpContext.Current.Cache is greater than zero.");
        }
        if (HttpContext.Current.Session == null)
        {
          builder.AppendLine("HttpContext.Current.Session is null.");
        }
        if (HttpContext.Current.Session != null)
        {
          builder.AppendLine("HttpContext.Current.Session is not null.");
        }
        if (HttpContext.Current.Session[Globals.SessionNameCompanyUser] == null)
        {
         builder.AppendLine("HttpContext.Current.Session[Globals.SessionNameCompanyUser] is null.");
        }
        if (HttpContext.Current.Session[Globals.SessionNameCompany] == null)
        {
          builder.AppendLine("HttpContext.Current.Session[Globals.SessionNameCompany] is null.");
        }
        if (HttpContext.Current.Session[Globals.SessionNameUser] == null)
        {
          builder.AppendLine("HttpContext.Current.Session[Globals.SessionNameUser] is null.");
        }
      }
      catch (Exception ex2)
      {
        //TODO:Temporary code.Need to remove 
        var path = @"C:\Exception\ExceptionWebService.txt";
       System.IO.Directory.CreateDirectory(@"C:\Exception");
        if (!System.IO.File.Exists(path))
        {
          System.IO.FileStream f = System.IO.File.Create(path);
          f.Close();
        }
        using (System.IO.StreamWriter writer = new System.IO.StreamWriter(path, true))
        {
          writer.WriteLine("-------------------------");
          writer.WriteLine(System.DateTime.Now.ToString());
          writer.WriteLine(builder.ToString());
         writer.WriteLine("-------------------------");
        }
      }
      throw ex1;
    }
        }
    }
    }

从HandleSaveOrderPostProcessing方法的第三条语句(IsCredentialsCached检查)抛出某些时间错误,该语句的执行在该方法的末尾没有任何错误。错误是对象引用未设置为对象的实例< / strong>。因此,我添加了catch块,在文件中写入日志,发现总是从条件 if(HttpContext.Current.Session!= null)中写入代码。

-------------------------
28-04-2020 11:36:37
HttpContext.Current is not null.
HttpContext.Current.User is not null.
HttpContext.Current.Cache is not null.
HttpContext.Current.Cache is greater than zero.
HttpContext.Current.Session is null.
-------------------------

可能是什么原因?请让我知道

谢谢 安息日

0 个答案:

没有答案