How to define a user agent in a WCF web service

How can I detect a user agent in a web service? My web service is implemented using the WCF web service with basicHTTPBinding. This will be a message from some SOAP clients. I want to know user agent from clients.

I would like to see some sample code for this.

I am using WCF-based web service, and in svc.cs I tried to catch this.Context.Request.UserAgent . But this leads to 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 

Change note:

I tried to activate ASP.NET compatibility mode. I added <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> to the configuration file and added [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] at the top of the class that implements the service interface. Then, using System.Web.HttpContext.Current.Request.UserAgent, gives me the user agent of his choice.

+6
web-services user-agent wcf
source share
5 answers

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

+3
source share

There is another way to get the user agent without including ASP.NET compatibility in web.config:

 string userAgent = WebOperationContext.Current.IncomingRequest.Headers["User-Agent"]; 
+12
source share

You can also use:

WebOperationContext.Current.IncomingRequest.UserAgent

+5
source share

What a completely useless answer!

This is not a trivial task. Yes, obviously, you can get the user-agent string, but how to actually do it? I spent 2 hours checking Google, etc., but found the answer buried in the MSDN documentation. In Visual Studio from inside WebMethod try

this.Context.Request.UserAgent

That should do it!

0
source share

User-Agent - standard HTTP header. It will be available for your web service as well as for any CGI-like.

Did you even bother to find this before posting your question? Google must have millions of hits.

-6
source share

All Articles