views:

297

answers:

3

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.

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?)

I also tried System.Web.HttpContext.Current.Request.UserAgent and it says:

'System.Web.HttpContext.Current' is null

Edit note:

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.

A: 

User-Agent is a standard HTTP header. It'll be available to your web service just like it's available to anything CGI-like.

Did you even bother searching for this before posting your question? There must be millions of hits for it on Google.

Steve Madsen
2 -1's and no comments explaining why? If I'm wrong about the HTTP header or this isn't true for a WCF webservice for some reason, say so and let's retag the question and give Kangkan an answer.
Steve Madsen
So, how can I catch the user agent on the server side? My service is on top of a WCF service (SVC). So can I detect the User Agent in the svc.cs page?
Kangkan
+1  A: 

What a totally unhelpful response!

This is not a trivial task. Yes it is obviously possible to get te user-agent string but how does one actually do it? I spent 2 hours checking google and so on but found the answer buried in MSDN documentation. In Visual Studio, from within a WebMethod try

this.Context.Request.UserAgent

That should do it!

Matthew Scott
@Mathew: thanks. I shall checkout and reply.
Kangkan
@Mathew: 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?) I also tried System.Web.HttpContext.Current.Request.UserAgent and it says: 'System.Web.HttpContext.Current' is null
Kangkan
your webservice (i.e. "this") should be derived from the base system.web.services.webservice which has the member Context (see http://msdn.microsoft.com/en-us/library/system.web.services.webservice_members.aspx) Did you leave out using system.web.services?
Matthew Scott
MSDN also says......XML Web service methods that have either the SoapRpcMethodAttribute or SoapDocumentMethodAttribute attribute applied to them with the OneWay property of set to true, do not have access to their HttpContext using the static Current property. To access the HttpContext, derive the class implementing the XML Web service method from WebService and access the Context property.This could be your issue
Matthew Scott
WCF services don't derive from System.Web.Services. They can be hosted outside of IIS (say, in a Windows Service).Orlangur's answer below is correct. If the service is hosted in IIS, you can enable ASP.NET compatability and then use the HttpContext.
TheNextman
+2  A: 

You can read user agent from the HttpContext.Current.Request object if you enable ASP.NET compatibility in web.config:

Orlangur
@Orlangur: Thanks, I shall try. But can you just guide me how to enable ASP.NET compatibility here?
Kangkan
I tried using HttpContext.Current.Request and the error that I got was "The name 'HttpContext' does not exist in the current context". I put <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> in the config file and [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] on top of the class. But it is not working.
Kangkan