wcf - 在 WCF RequestInterceptor 中访问 HttpContext

标签 wcf httpcontext wcf-rest

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

最佳答案

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

  • 将 AspNetCompatibilityRequirementsAttribute 应用于您的服务类并将RequirementsMode 属性设置为Required
  • 确保通过配置以下内容启用兼容性:
    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled=”true” />
    </system.serviceModel>
    

  • 完成后,您可以随时使用 the static Current property 访问当前的 HttpContext 实例。 .例如:
    foreach(HttpCookie cookie in HttpContext.Current.Request.Cookies)
    {
        /* ... */
    }
    

    请注意,启用与 ASP.NET 运行时的集成确实会为每个请求带来一些额外的开销,因此如果您不需要它,您可以通过不启用它而只使用 System.ServiceModel.Web 运行时来节省一些性能。您可以使用 HttpRequestResponseMessageProperty 访问几乎所有您需要的信息。和 HttpResponseMessageProperty类。

    有关该主题的更多信息,请参阅 this section of MSDN titled WCF and ASP.NET .

    关于wcf - 在 WCF RequestInterceptor 中访问 HttpContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1630279/

    相关文章:

    wcf - 保护 WCF 使用的命名管道

    asp.net-mvc - 使用 WCF/OData 作为访问层而不是直接使用 EF/L2S/nHibernate 的论点

    asp.net - 不使用 httpcontext 获取应用程序路径。 (asp.net)

    wcf-rest - Kendo 网格内联编辑

    Silverlight PollingDuplex InnerChannel 出现 multipleMessagesPerPoll (serverPollTimeout) 故障

    java - 使用 WSHttpBinding 的 WCF 和 Java Interop,

    c# - 从 HTTPContext 获取正确的编码

    asp.net - HttpContext.Current如何工作?

    c# - WCF Restful Web 服务客户端限制

    wcf - 如何在 WCF REST 中调整默认的 JSON 序列化程序