How to detect user agent in WCF web service - wcf

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.

There is another way to get the user agent without enabling ASP.NET compatibility in web.config:
string userAgent = WebOperationContext.Current.IncomingRequest.Headers["User-Agent"];

You can use also:
WebOperationContext.Current.IncomingRequest.UserAgent

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

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!

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.

Related

Cleint not receive response: “No handlers could be found for message type”

I have a .net core service that needs to send a request via nservicebus.
I resolve IMessageSession with DI and send a request like this:
var response = await messageSession.Request<CreateDeviceResponse>(request);
In the logs I see that another service received this request and send a reply:
The problem that I never receive a response.
Client receive such errors:
I know that such an issue can occur if the client and server endpoint names are same, but I checked and I use different names
asp net mvc 4.7.2
First of all, the message isn't lost, but in the error queue.
I suggest to follow the tutorial to understand how NServiceBus works and what you need to have in place where. The tutorials start here but an entire learning path starts here and it explains broader, including about general concepts and architecture.
Like #tchelidze mentions, you need to have an implementation of the IHandleMessages<CreateDeviceReponse> interface, like is outlined here in the tutorial. Also, it doesn't matter to which endpoint you send it to. EndpointA can send to both EndpointB and EndpointA. What you cannot do is have two endpoints with the same name. It's like having two computers with the same name called MachineA and then from MachineB try to access files on MachineA. Which of the two should MachineB connect to? There is something else called 'competing consumer pattern', but that's of later concerns ;-)
If you need more help, feel free to ask at https://discuss.particular.net/ where more NServiceBus community members are located, as well as engineers of NServiceBus itself.
For some reason, it helped to make a request inside a task:
Task.Run(async () => await messageSession.Request<CreateDeviceResponse>(request))
I still not understand what is the problem, but it works only with such implementation.
In .net core web API, it works even without Task.Run, so I believe it has something to do with the fact that I am making this request in asp net mvc 4.7.2

sharepoint object model in webservice

i am creating a webservice to provide one of the user poperty value to another application.
to do the same i am using sharepoint object model to connect to the mysite.
code : spsite site = new site(mysite url)
but i get the below error, kindly help.
error:
System.IO.FileNotFoundException: The Web application at "mysite url" could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
If this is a WCF service then by default there is no httpcontext, ergo, no spcontext. The fix is to enable "asp.net compatibility mode" in web.config. This is done with the following directive:
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
I can think of two things that might be the cause of your problem:
-If your SharePoint is 64 bits, your application has to be built in 64 bit. The same applies for x86.
-The Target Framework option in your application needs to be set to 3.5
EDIT: I found an article about it. Maybe you could benefit from it.
http://blogs.technet.com/b/stefan_gossner/archive/2011/09/19/common-issue-new-spsite-returns-quot-the-web-application-at-http-server-port-could-not-be-found-quot.aspx

Can WCF REST (WebHttpBinding) honor PROGRAMMATIC outputcache policies?

I know all about the AspNetCacheProfileAttribute. But is there any way to hook into the cache programmatically? I've tried using Response.Cache in global.asax which seems to set the correct client-side headers but the response is never cached on the server.
I don't think you can do it unless you build your own solution. I just checked implementation of AspNetCahceProfileAttribute which only add internal CachingParameterInspector to the operation dispatcher. This has two problems:
Parameter inspector is assigned when the service host starts = during first request and until that it cannot be changed
Parameter inspector is initialized in its cosntructor by reading cache configuration from the web.config file based on the profile name passed to AspNetCacheProfileAttribute
There is no API available to modify already assigned and configured parameter inspector

WCF Server Error. The content type text/html of the response message does not match the content type of the binding

I've got a WCF Service, which calls a webservice, running on my development IIS server (IIS 7). I've added it as a service reference to a C# Website Project and it adds fine.
However, when I try to call any of the service contracts, I get the following error:
The content type text/html
of the response message does not match
the content type of the binding
(text/xml; charset=utf-8). If using a
custom encoder, be sure that the
IsContentTypeSupported method is
implemented properly. The first 1024
bytes of the response were:
'Blocked Web
Page
thanks in advance
BB
The error message says it clearly: you're getting back an HTML page instead of your service response. Looking at the fragment of that page listed in the error message, you're probably not authroized to use that service.
Try to connect to the service URL in a browser - you should probably see a page explaining that you're not allowed to access the page. Most likely, this is a permissions issue.
You need to configure WCF Tracing and find out what's happening on the server side.

404 not found when accessing JSON WCF Service with jQuery

I have a WCF service hosted in IIS which seems to be running fine but am unable to access it through jQuery on the client side.
My site structure is like this -
localhost:9080/Website
localhost:9080/WCFService
I've set the anonymous authentication user id to that of the AppPool identity.
When I try to access a GET method from the browser, it returns the JSON data just fine. But when I try to access the same method from the client using jQuery $.ajax() the server returns a 404 error.
The URL of the WCF method is
localhost:9080/WCFService/Service.svc/SimpleMethod
The signature of the method is
string SimpleMethod()
I've already tried looking articles online and I don't think the web.config is any different. It looks like a permissions thing to me.
Can anyone shed any light on this issue?
-Thanks!
Here's what I did -
Added an endpoint behavior which uses <webHttp /> and removed <enableWebScript />. That fixed it.
When I try to access a GET method from
the browser, it returns the JSON data
just fine
You are accessing from the server pc and using http://localhost:9080
But when I try to access the same
method from the client using jQuery
$.ajax() the server returns a 404
error.
What URL are you using for ajax call? Use relative URL.
It looks like a permissions thing to
me.
If on same machine (server machine) invoking directly from browser works but invoking using ajax from same client does not work, it can be a wrong url
You better post your ajax calling code.