How to initialize a custom HTTP context or HttpContextBase

I am experimenting with creating my own custom HTTP context:

CustomHttpContext : HttpContextBase
{
    public override HttpRequestBase Request { }
}

One thing that I cannot understand is to initialize the base class with

System.Web.HttpContext.Current

Does anyone have any ideas how I can initialize the user context first with Current Http and then override certain methods / properties to serve my own purpose?

+5
source share
2 answers

- , . , HttpContext HttpContextBase, IServiceProvider. , HttpContext , , , -, .

, , HttpContextBase, , , HttpContext!

'decompiler', HttpContext.Current:

// System.Web.HttpContext
/// <summary>Gets or sets the <see cref="T:System.Web.HttpContext" /> object for the current HTTP request.</summary>
/// <returns>The <see cref="T:System.Web.HttpContext" /> for the current HTTP request.</returns>
public static HttpContext Current
{
    get
    {
        return ContextBase.Current as HttpContext;
    }
    set
    {
        ContextBase.Current = value;
    }
}

ContextBase.Current( System.Web.Hosting.ContextBase):

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

CallContext ( System.Runtime.Messaging):

// System.Runtime.Remoting.Messaging.CallContext
/// <summary>Gets or sets the host context associated with the current thread.</summary>
/// <returns>The host context associated with the current thread.</returns>
/// <exception cref="T:System.Security.SecurityException">The immediate caller does not have infrastructure permission. </exception>
public static object HostContext
{
    [SecurityCritical]
    get
    {
        IllogicalCallContext illogicalCallContext = Thread.CurrentThread.GetIllogicalCallContext();
        object hostContext = illogicalCallContext.HostContext;
        if (hostContext == null)
        {
            LogicalCallContext logicalCallContext = CallContext.GetLogicalCallContext();
            hostContext = logicalCallContext.HostContext;
        }
        return hostContext;
    }
    [SecurityCritical]
    set
    {
        if (value is ILogicalThreadAffinative)
        {
            IllogicalCallContext illogicalCallContext = Thread.CurrentThread.GetIllogicalCallContext();
            illogicalCallContext.HostContext = null;
            LogicalCallContext logicalCallContext = CallContext.GetLogicalCallContext();
            logicalCallContext.HostContext = value;
            return;
        }
        LogicalCallContext logicalCallContext2 = CallContext.GetLogicalCallContext();
        logicalCallContext2.HostContext = null;
        IllogicalCallContext illogicalCallContext2 = Thread.CurrentThread.GetIllogicalCallContext();
        illogicalCallContext2.HostContext = value;
    }
}

, HttpContext. , , - ( !). , , (. ).

, HttpContext.Current , HttpContext, . BCL, , .

, , HttpContext CustomContext. HttpContext.Current BaseContext, ( , , ).

, .. HTTP /.

. :

+7

All Articles