6
4

1 回答 1

9

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

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

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

完成此操作后,您可以随时使用静态 Current 属性访问当前的 HttpContext 实例。例如:

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

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

有关该主题的更多信息,请参阅MSDN 中标题为 WCF 和 ASP.NET 的这一部分

于 2009-10-27T13:54:30.603 回答