gpt4 book ai didi

wcf - 在WCF服务中HttpContext.Current是否不应该为null?

转载 作者:行者123 更新时间:2023-12-04 03:35:09 26 4
gpt4 key购买 nike

我以为WCF服务中的HttpContext.Current is supposed to be null(即使启用了aspNetCompatibilityEnabled)。

MSDN: HttpContext: Current is always null when accessed from within a WCF service. from http://msdn.microsoft.com/en-us/library/aa702682.aspx



我在web.config中有这个:
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true">

但是这个 :
 HttpContext.Current + " has file path of " + HttpContext.Current.Request.FilePath

返回有效的上下文(并证明我在.svc文件中)
 "System.Web.HttpContext has file path of /rrmvc/MVCServices/OrderPipelineService.svc"

我基本上需要一种方法来判断我是否在SVC或ASMX中。我该怎么办?

最佳答案

不,当启用ASP.NET兼容性时,这很重要-WCF服务是在ASP.NET管道的上下文中执行的,并且您具有对ASP.NET服务(如缓存, session 状态,窗体)的完全访问权限认证等

如果您需要确定当前请求是针对WCF服务还是ASMX服务,则可以:

  • 检查您已经在实验中转储的字符串的结尾
  • 检查HttpContext.Current.Handler

  • 请注意,在未启用ASP.NET兼容性的情况下,在所有情况下您都不会拥有HttpContext.Current。在这种情况下,您可以确定自己不在ASMX Web服务范围内运行。

    链接页面的节选:以ASP.NET兼容模式托管WCF服务

    Unlike the default side-by-side configuration, where the WCF hosting infrastructure intercepts WCF messages and routes them out of the HTTP pipeline, WCF services running in ASP.NET Compatibility Mode participate fully in the ASP.NET HTTP request lifecycle. In compatibility mode, WCF services use the HTTP pipeline through an IHttpHandler implementation, similar to the way requests for ASPX pages and ASMX Web services are handled. As a result, WCF behaves identically to ASMX with respect to the following ASP.NET features:

    HttpContext: WCF services running in ASP.NET Compatibility Mode can access Current and its associated state.

    关于wcf - 在WCF服务中HttpContext.Current是否不应该为null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2076493/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com