【问题标题】:HttpContext.Current is null in WCF Data Service hosted in Windows ServiceHttpContext.Current 在 Windows 服务中托管的 WCF 数据服务中为空
【发布时间】:2015-12-02 05:08:47
【问题描述】:

我在 Windows 服务中创建了 WCF 数据服务并尝试访问 HttpContext。

我将此添加到我的配置文件中:

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

但是,当我尝试访问它时,它为空。

protected override void OnStartProcessingRequest(ProcessRequestArgs args)
    {
      base.OnStartProcessingRequest(args);   
      HttpContext httpContext = HttpContext.Current;
      File.AppendAllText(@"c:\Temp\ERROR.log",
      httpContext != null
       ?"HTTPCONTEXT IS NOT NULL"
       :"HTTPCONTEXT IS NULL");
    }

我还应该设置什么?

【问题讨论】:

    标签: c# wcf-data-services httpcontext


    【解决方案1】:

    我找到了答案,恐怕是这样:

    禁用的 ASP.NET HTTP 功能是:

    HttpContext.Current:在此模式下始终为 null。对于 ASMX 服务,这是存储在线程本地存储 (TLS) 中的 ThreadStatic 属性。 WCF 提供了此功能的对应项:OperationContext.Current。

    来源:http://blogs.msdn.com/b/wenlong/archive/2006/01/23/516041.aspx

    【讨论】:

    • 正确 - 如果您将 WCF 服务托管在 Windows 服务中,则显然没有 ASP.NET 组件,因此不涉及 HttpContext .... WCF ASP.NET - 这是一种完全不同的技术
    • 是的,我意识到了这一点。我仍在寻找有关如何在传出流上使用 gzip 的一些信息。 HttpContext 本来不错的...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多