Workaround for HttpContext.HideRequestResponse being internal? Detect if HttpContext.Request is really available?(HttpContext.HideRequestResponse 的解决方法是内部的?检测 HttpContext.Request 是否真的可用?)
问题描述
我们正在迁移应用程序以使用 IIS7 集成模式.在设计为在 HTTP 请求的上下文中或不在 HTTP 请求的上下文中工作的库代码中,我们通常有这样的代码:
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 集成模式下,只要从 Application_Start
调用此代码,对 HttpContext.Current.Request
的检查就会引发异常.
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.
查看 Reflector 中的 HttpContext
我看到它有一个 internal bool 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
,但是如何在通用库代码中确定使用 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
?
http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart/
我正在使用那里提到的解决方法,即使用 Application_BeginRequest
和 initialized
字段作为 BeginRequest
,但这必须在每个调用应用程序中完成,而我更愿意使库代码更健壮并处理这种情况,而不管它是从哪里调用的.
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.
您可以使用您在答案中描述的反射,但您不想这样做,因此受到 Microsoft 提供的 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.
如果您决定使用反射,请注意 HttpApplication.InitInternal
方法,该方法设置 HideRequestResponse 标志.
If you do decide to use reflection, of note is the HttpApplication.InitInternal
method which is what sets the HideRequestResponse flag.
希望对您有所帮助.我建议您向 Microsoft Connect 提交报告.
Hope that helps. I would suggest you file a report with Microsoft Connect.
这篇关于HttpContext.HideRequestResponse 的解决方法是内部的?检测 HttpContext.Request 是否真的可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:HttpContext.HideRequestResponse 的解决方法是内部的?检测 HttpContext.Request 是否真的可用?
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01