如何在 WCF Web 服务中检测用户代理 [英] How to detect user agent in WCF web service
问题描述
如何检测 Web 服务中的用户代理?我的 Web 服务是使用带有 basicHTTPBinding 的 WCF Web 服务实现的.它将是来自一些 SOAP 客户端的帖子.我想从客户那里了解用户代理.
How can I detect the user agent in a web service? My web service is implemented using a WCF webservice with basicHTTPBinding. It will be a post from some SOAP clients. I wish to know the user-agent from the clients.
我希望看到一些示例代码.
I shall like to see some sample code for this.
我正在使用基于 WCF 的 Web 服务,并且在 svc.cs 中,我尝试捕获 this.Context.Request.UserAgent
.但它给出了以下错误:
I am using a WCF based web service and in the svc.cs, I tried to catch this.Context.Request.UserAgent
. But it gives the following error:
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 根据需要为我提供用户代理.
I tried to activate the ASP.NET compatibility mode. I added <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
in the config file and added [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
on the top of the class that implements the service interface. Then using System.Web.HttpContext.Current.Request.UserAgent gives me the user agent as desired.
推荐答案
如果在 web.config 中启用 ASP.NET 兼容性,则可以从 HttpContext.Current.Request 对象读取用户代理:
You can read user agent from the HttpContext.Current.Request object if you enable ASP.NET compatibility in web.config:
这篇关于如何在 WCF Web 服务中检测用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!