【问题标题】:How to detect user agent in WCF web service如何在 WCF Web 服务中检测用户代理
【发布时间】:2010-05-03 17:20:35
【问题描述】:

如何检测 Web 服务中的用户代理?我的 Web 服务是使用带有 basicHTTPBinding 的 WCF Web 服务实现的。这将是一些 SOAP 客户端的帖子。我想知道客户的用户代理。

我希望看到一些示例代码。

我正在使用基于 WCF 的 Web 服务,并且在 svc.cs 中,我试图捕获 this.Context.Request.UserAgent。但它给出了以下错误:

this.Context.Request.UserAgent 'MySoapService.MyService' does not contain a definition for 'Context' and no extension method 'Context' accepting a first argument of type 'MySoapService.MyService' could be found (are you missing a using directive or an assembly reference?)

我也试过System.Web.HttpContext.Current.Request.UserAgent,它说:

'System.Web.HttpContext.Current' is null

编辑说明:

我尝试激活 ASP.NET 兼容模式。我在配置文件中添加了<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />,并在实现服务接口的类的顶部添加了[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]。然后使用 System.Web.HttpContext.Current.Request.UserAgent 为我提供所需的用户代理。

【问题讨论】:

  • 这将是一些 SOAP 客户端的帖子。

标签: wcf web-services user-agent


【解决方案1】:

还有另一种获取用户代理的方法,无需在 web.config 中启用 ASP.NET 兼容性:

string userAgent = WebOperationContext.Current.IncomingRequest.Headers["User-Agent"];

【讨论】:

  • 谢谢。如果我能在不使用 ASP.NET 兼容性的情况下做到这一点,那就太好了。
【解决方案2】:

你也可以使用:

WebOperationContext.Current.IncomingRequest.UserAgent

【讨论】:

    【解决方案3】:

    如果在 web.config 中启用 ASP.NET 兼容性,则可以从 HttpContext.Current.Request 对象读取用户代理:

    【讨论】:

    • 谢谢,我会试试的。但是你能指导我如何在这里启用 ASP.NET 兼容性吗?
    • 我尝试使用 HttpContext.Current.Request 并且我得到的错误是“名称 'HttpContext' 在当前上下文中不存在”。我将 放在配置文件中,并将 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 放在类的顶部。但它不起作用。
    • 我觉得你也可以使用 OperationContext.Current.RequestContext.RequestMessage.Headers
    【解决方案4】:

    多么无益的回应!

    这不是一项简单的任务。是的,显然有可能获得用户代理字符串,但实际上是如何做到的呢?我花了 2 个小时检查 google 等,但在 MSDN 文档中找到了答案。在 Visual Studio 中,从 WebMethod 中尝试

    this.Context.Request.UserAgent

    应该这样做!

    【讨论】:

    • @Mathew:我正在使用基于 WCF 的 Web 服务,并且在 svc.cs 中,我试图捕捉 this.Context.Request.UserAgent。但它给出了以下错误: this.Context.Request.UserAgent 'MySoapService.MyService' 不包含'Context' 的定义,并且没有扩展方法'Context' 接受'MySoapService.MyService' 类型的第一个参数可以找到(您是否缺少 using 指令或程序集引用?)我还尝试了 System.Web.HttpContext.Current.Request.UserAgent,它说:'System.Web.HttpContext.Current' is null
    • 您的网络服务(即“this”)应该从具有成员上下文的基本 system.web.services.webservice 派生(请参阅msdn.microsoft.com/en-us/library/…)您是否使用 system.web.services ?
    • MSDN 还说......将 SoapRpcMethodAttribute 或 SoapDocumentMethodAttribute 属性应用于它们且 OneWay 属性设置为 true 的 XML Web 服务方法无法使用静态当前属性。要访问 HttpContext,请从 WebService 派生实现 XML Web 服务方法的类并访问 Context 属性。这可能是您的问题
    • WCF 服务不是从 System.Web.Services 派生的。它们可以托管在 IIS 之外(例如,在 Windows 服务中)。 Orlangur 在下面的回答是正确的。如果服务托管在 IIS 中,您可以启用 ASP.NET 兼容性,然后使用 HttpContext。
    • -1:因为在发布这样的答案时没有三思而后行,也没有更多地关注WCF服务的问题。
    【解决方案5】:

    User-Agent 是标准的 HTTP 标头。它对您的网络服务可用,就像它对任何 CGI 类的东西可用一样。

    在发布您的问题之前,您是否甚至费心搜索此内容?在 Google 上一定有数百万次点击。

    【讨论】:

    • 2 个 -1 且没有 cmets 解释原因?如果我对 HTTP 标头有误,或者出于某种原因这对于 WCF Web 服务不正确,请说出来,让我们重新标记问题并给 Kangkan 一个答案。
    • 那么,如何在服务器端捕获用户代理呢?我的服务位于 WCF 服务 (SVC) 之上。那么我可以在 svc.cs 页面中检测到 User Agent 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多