访问 WCF RequestInterceptor 中的 HttpContext

发布于 2024-08-08 21:02:29 字数 499 浏览 12 评论 0原文

我正在使用 WCF REST stater 工具包 构建一个简单的 xml HTTP 服务。作为此部分的一部分,我使用 RequestInterceptor 进行身份验证。在 RequestInterceptor 内部,我可以访问 System.ServiceModel.Channels.RequestContext 对象,从中我可以获取请求 url、查询字符串参数和其他有用的东西。我无法解决的是如何访问请求的 HttpContext。我在 HttpContext 中存储了一些内容,我想在 requestInterceptor 中访问它们,但我很难获取它们。当我在 Visual Studio 中使用快速监视时,我可以看到它隐藏在 requestContext 的私有成员中。有人可以告诉我如何访问 HttpContext,也许使用 RequestContext 对象上的反射吗?

I am using the WCF REST stater kit to build a plain xml over HTTP service. As part of this Im using a RequestInterceptor to do authentication. Inside of the RequestInterceptor I have access to a System.ServiceModel.Channels.RequestContext object from which i can get the request url, querystring params and other helpful things. What I cannot work out is how to get access to the HttpContext of the request. I have several things stored in the HttpContext which I want to access inside the requestInterceptor but Im struggling to get to them. When I use the quickwatch inside Visual Studio I can see that it is there buried inside private members of the requestContext. Can somebody show me how to access the HttpContext, perhaps using reflection on the RequestContext object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糖 2024-08-15 21:02:29

只要打开兼容性,您就可以在 ASP.NET 托管的任何 WCF 服务内访问 ASP.NET 的 HttpContext。这通过两个步骤完成:

  1. 将 AspNetCompatibilityRequirementsAttribute 应用到您的服务类,并将 requestsMode 属性设置为必需
  2. 确保通过配置以下内容启用兼容性:

    ;
        >
    
    

完成此操作后,您可以随时使用 静态 Current 属性。例如:

foreach(HttpCookie cookie in HttpContext.Current.Request.Cookies)
{
    /* ... */
}

请注意,启用与 ASP.NET 运行时的集成确实会为每个请求带来一些额外的开销,因此,如果您不需要它,则可以通过不启用它而仅使用 System.ServiceModel.Web 运行时来节省一些性能。您可以使用 HttpRequestResponseMessagePropertyHttpResponseMessageProperty类。

有关该主题的详细信息,请参阅 MSDN 中标题为 WCF 和 ASP.NET 的部分

You can access ASP.NET's HttpContext inside any WCF service hosted in ASP.NET as long as you turn on compatibility. This is done in two steps:

  1. Apply the AspNetCompatibilityRequirementsAttribute to your service class and set the RequirementsMode property to Required
  2. Make sure you enable compatibility by configuring the following:

    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled=”true” />
    </system.serviceModel>
    

Once you've done that, you can access the current HttpContext instance at any time using the static Current property. For example:

foreach(HttpCookie cookie in HttpContext.Current.Request.Cookies)
{
    /* ... */
}

Note that enabling integration with the ASP.NET runtime does incur some additional overhead for each request, so if you don't need it you can save some performance by not enabling it and just using the System.ServiceModel.Web runtime instead. You have access to pretty much all the information you need using the HttpRequestResponseMessageProperty and HttpResponseMessageProperty classes.

For more information on the subject, see this section of MSDN titled WCF and ASP.NET.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文