解决方法HttpContext.HideRequestResponse是内部?检测是否HttpContext.Request是真货吗? [英] Workaround for HttpContext.HideRequestResponse being internal? Detect if HttpContext.Request is really available?

查看:230
本文介绍了解决方法HttpContext.HideRequestResponse是内部?检测是否HttpContext.Request是真货吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们要迁移到使用IIS7集成模式的应用程序。在库code,它被设计为工作,要么一个HTTP请求的上下文或内没有,我们常见的有code是这样的:

We're migrating an application to use IIS7 integrated mode. In library code that is designed to work either within the context of an HTTP request or not, we commonly have code like this:

if (HttpContext.Current != null &&
    HttpContext.Current.Request != null) {

    // do something with HttpContext.Current.Request

} else {

    // do equivalent thing without HttpContext..

}

但在IIS7集成模式为 HttpContext.Current.Request 检查抛出每当这个code从的Application_Start

But in IIS7 integrated mode the check for HttpContext.Current.Request throws an exception whenever this code is called from Application_Start.

protected void Application_Start(object sender, EventArgs e)
{
    SomeLibrary.DoSomethingWithHttpContextCurrentDetection();
}

结果:

System.Web.HttpException:请求不可用在这种情况下

System.Web.HttpException: Request is not available in this context

我如何可以检测该请求是否确实可用的无异常处理程序包装这些呼吁并采取基于是否产生或没有异常行动。

How can I detect whether the request is really available without wrapping these calls in an exception handler and taking action based on whether an exception is generated or not.

在反射综观的HttpContext 我看到它有一个内部布尔HideRequestResponse 的领域,但它的内部,所以我只能得到它与反思,这就是脆弱。是否有更多的官方/认可的方法,以确定它是否可以调用 HttpContext.Request

Looking at HttpContext in Reflector I see it has an internal bool HideRequestResponse field but it's internal so I can only get to it with reflection and that's fragile. Is there a more official/approved way to determine if it's ok to call HttpContext.Request?

这对上述博客文章说,不使用的HttpContext ,但如何在通用库code,你能确定它是否可以使用的HttpContext

This blog post about the subject says not to use HttpContext, but how, in generic library code, can you determine if it's ok to use HttpContext?

<一个href=\"http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart/\" rel=\"nofollow\">http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart/

我使用的变通提到有哪些是使用的Application_BeginRequest 初始化字段,只一旦初始化作为的BeginRequest 的一部分,但在而我倒是preFER使图书馆code更强大,处理每调用应用程序做这种情况下,无论在哪里,它是从所谓的。

I'm using the work-around mentioned there which is to use Application_BeginRequest and an initialized field to only initialize once as part of BeginRequest, but that has to be done in every calling application whereas I'd prefer to make the library code more robust and handle this situation regardless of where it's called from.

推荐答案

恐怕答案是您不能的得到你想要的东西 - 微软认为这种情况下,作为一个特殊情况所以它会抛出异常。

I'm afraid the answer is that you can't get what you want - Microsoft sees this case as an 'exceptional circumstance' and so it will throw an exception.

您可以使用反射,你在你的答案形容,但你不想等是由微软提供,或好或坏的API限制。

You can use reflection as you describe in your answer but you don't want to and so are limited by the API that Microsoft have provided, for better or for worse.

如果你决定使用反射的说明是这是什么设置HideRequestResponse标志。在 HttpApplication.InitInternal

If you do decide to use reflection, of note is the HttpApplication.InitInternal method which is what sets the HideRequestResponse flag.

希望有所帮助。我建议您向微软连接的报告。

Hope that helps. I would suggest you file a report with Microsoft Connect.

这篇关于解决方法HttpContext.HideRequestResponse是内部?检测是否HttpContext.Request是真货吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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