Why is HttpContext.Current static?

I'm a little confused why is HttpContext.Current a static property? If the runtime processes more than 1 request at a time, do not all requests see the same Current value, since it is static? or it is processed by the framework using some kind of synchronization technique, and if so, why is the static not a normal property.

Missing something?

+4
source share
1 answer

Here is the property implementation Current:

public static HttpContext Current
{
  get
  {
    return ContextBase.Current as HttpContext;
  }
  set
  {
    ContextBase.Current = (object) value;
  }
}

And the ContextBase that is used in this property:

internal class ContextBase
{
  internal static object Current
  {
    get
    {
      return CallContext.HostContext;
    }
    [SecurityPermission(SecurityAction.Demand, Unrestricted = true)] set
    {
      CallContext.HostContext = value;
    }
  }

And CallContext:

public sealed class CallContext
{

  public static object HostContext
  {
    [SecurityCritical] 
    get
    {
      ExecutionContext.Reader executionContextReader = 
         Thread.CurrentThread.GetExecutionContextReader();
      return executionContextReader.IllogicalCallContext.HostContext ?? executionContextReader.LogicalCallContext.HostContext;
    }
    [SecurityCritical] 
    set
    {
      ExecutionContext executionContext = 
         Thread.CurrentThread.GetMutableExecutionContext();
      if (value is ILogicalThreadAffinative)
      {
        executionContext.IllogicalCallContext.HostContext = (object) null;
        executionContext.LogicalCallContext.HostContext = value;
      }
      else
      {
        executionContext.IllogicalCallContext.HostContext = value;
        executionContext.LogicalCallContext.HostContext = (object) null;
      }
    }

CallContext.HostContext, Thread.CurrentThread , \request.

HttpContext \Controller. , , - , . HttpContext.Current , .

+6

Source: https://habr.com/ru/post/1548497/


All Articles