如何初始化自定义HTTP上下文或HttpContextBase [英] How to initialise a custom HTTP Context or HttpContextBase

查看:1972
本文介绍了如何初始化自定义HTTP上下文或HttpContextBase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与创建自己的自定义HTTP上下文实验:

I am experimenting with creating my own custom HTTP Context:

CustomHttpContext : HttpContextBase
{
    public override HttpRequestBase Request { }
}

有一件事情我无法弄清楚是怎么初始化与

One thing i can't figure out is how to initialize the base class with

System.Web.HttpContext.Current

没有人有任何想法如何,我可以用当前的HTTP先初始化自定义背景,然后覆盖某些方法/属性服务于我自己的目的是什么?

Does anyone have any ideas how i can initialise the custom context first with the Current Http then override certain Methods/Properties to serve my own purpose?

推荐答案

简单的答案是否定的,这是不可能的。还要注意的是HttpContext的不从HttpContextBase继承,相反,他们都实现的IServiceProvider。最后,HttpContext的是密封的,这表明作者不希望人们做其他的比消耗这一类东西。

The simple answer is no, it's not possible. Also note that HttpContext does not inherit from HttpContextBase, instead, they both implement IServiceProvider. Finally, HttpContext is sealed, suggesting that the authors did not want people to do anything other than consume this class.

由于您受到HttpContextBase无疑惹恼了一个无参数的构造函数!所以甚至不给你当前请求和响应喜欢的HttpContext实例化的选择。

As you are no doubt annoyed by HttpContextBase has a parameterless constructor so does not even give you the option of instantiating it from the current request and response like HttpContext!

让我们使用的闪客来看看HttpContext.Current执行:

Let's use a 'decompiler' to take a look at the implementation of 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):

If we take a look at ContextBase.Current (from 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):

and CallContext (in 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的是怎样的感觉被检索。它被包装在当他们造访的网站当前用户启动的线程(这是非常合情合理的!)。进一步的深入研究,我们可以看到它也被每个请求重新创建(见下文)。

We start to get a feel for how the HttpContext is being retrieved. It's being packaged in with the thread the current user started when they visted the website (which makes perfect sense!). Delving further we can see it also gets recreated per request (see below).

我们也可以看到,在界面层,HttpContext.Current无法改变你自己的HttpContext点作为属性不是虚拟的。它还使用了许多BCL类是私有或内部,所以你不能简单地复制大部分的实现。

We can also see, at the interface layer, HttpContext.Current cannot be changed to point at your own HttpContext as the property is not virtual. It also uses many BCL classes that are private or internal so you can't simply copy most of the implementation.

什么会更容易,也不易发生其他任何问题是简单地包裹HttpContext的用自己的CustomContext对象。你可以简单地包裹HttpContext.Current在BaseContext属性,则对类自己的属性(和使用任何会话,数据库,或者你想存储和检索自己的属性基于请求状态存储机制)。

What would be easier, and also less prone to any other issues would be to simply wrap HttpContext with your own CustomContext object. You could simply wrap HttpContext.Current in a BaseContext property, then have your own properties on the class (and use whatever session, database, or request based state storage mechanism you want to store and retrieve your own properties).

就个人而言,我会用我自己的类来存储我自己的信息,因为它属于我的应用程序和用户等,并是不是真的任何事情做的HTTP 。管道或请求/响应处理

Personally, I'd use my own class for storing my own information, as it belongs to my application and user etc and isn't really anything to do with the http pipeline or request/response processing.

另请参阅:

  • ASP.NET MVC : How to create own HttpContext
  • How is HttpContext being maintained over request-response

这篇关于如何初始化自定义HTTP上下文或HttpContextBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆