Does WebMessageFomat.Json will work only for WebHTTPBinding? - asp.net

I am creating a WCF service and I am planning to set the message format as JSON. But when I use BasicHttpBinding, I get the response and request in XML format. I am not sure does that mean this WebMessageFormat.Json can be used only in WEBHttpBinding (REST)? I have an understanding that SOAP uses only XML but this MessageFormat attribute arise a question in me whether JSON format can be used across all binding.

Related

.NET soap web service that returns JSON- how does it work?

I don't have something in particular that I need to implement, I am just trying to understand some concepts.
As far as I know, .NET web method (for example), that is inside a web service, is using the SOAP protocol. that means that the data received to the server needs to be in XML format, and the data that is returned from the web method should also be in XML format.
I know I can write something like this:
[WebMethod]
public static Object someMethod(Object josn)
{
Console.Write(json("someProperty"));
return new {name = "John"}
}
I know that data sent back to the client is in JSON format...
but if we are using the SOAP protocol, shouldn't it return XML?
I want to understand what is happening behind the scene or what am I missing, does the request was sent in xml format and the .NET framework knows how to extract the parameters that were sent, if so- how can I retrieve the full xml that represents the request made?
What you are referring to is called the SOAP envelope. Take a look here:
Sample SOAP Envelope
One of the easiest ways to see the SOAP envelope that is being used in a request or response is to use Fiddler or Postman.
EDIT
Here's an example of WCF SOAP envelope with JSON (not sure if you are using WCF or the old .NET Web Service with ASMX files):
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header></s:Header>
<s:Body>[{"DateOfBirth":"\/Date(286801200000+1300)\/","FirstName":"Foo","Id":1,"LastName":"Bar"},{"DateOfBirth":"\/Date(333720000000+1200)\/","FirstName":"Foo","Id":1,"LastName":"Bar"}]</s:Body>
</s:Envelope>

What are the best practices for returning arbitrary data from an ASP.NET MVC or ASP.NET Web API

I am building an asp.net (MVC/Web API) application where I will be returning arbitrary data from a view, and the type of data is not known at design time.
Specifically, the user will specify some URI, and my back end will fetch that uri. The data returned might be a straight text/html web page, or it could return an RSS feed with a text/rss+xml content type, or it might return a JSON payload, etc. I won't know the format of the data until runtime, after I fetch the data and inspect the content-type.
My objective (for all intents and purposes - irrelevant detail omitted) is to echo that payload back to the user. I will of course set the proper content type in the response as well as any relevant headers (like cookies) that were in the original response.
What is the best practice for returning the data from the controller action to accommodate this? If I were using ASP.NET Web API for example, could I just send (and how?) an arbitrary blob (I use 'blob' for lack of a better term - what I mean is that I will echo the payload of the http response I got when I made the call to the resource) back to the client (of course with the content-type set properly)?
In summary my specific questions are:
Should I use ASP.NET MVC or ASP.NET Web API to accomplish this?
If ASP.NET Web API (I assuming that is the proper direction), how do I echo an arbitrary stream (I say stream rather than string, because it could be binary) of data (of which I do not know the content-type until runtime) to the client?

Overriding HTTP request headers via query string

We have a client of a ServiceStack service that cannot easily send the correct value for some request headers (such as Accept or Accept-Encoding).
Is there any mechanism in ServiceStack (or ASP.NET) that can allow the client to use query string parameters to override the value of request headers, in a generic way?
The format URL param recognized by ServiceStack is close, but it seems to not help in this specific case (the client needs to send Accept: image/png, which seems to be ignored by the format param).
Or another way to look at this, is there a way to name or annotate the properties of a GET request DTO so that ServiceStack will populate those properties with the values of request headers during deserialization? This could provide an alternative approach for solving this problem.

If SOAP is an XML based protocal used by WebServices why an .asmx service returns JSON?

I am confused on one thing. As I have read web services use SOAP protocol for communication and it is an XML based protocol; I was expecting the classical HelloWorld method on an asp.net web service to return an xml based data.
But when I checked with Fiddler i see that the Response Textview has a JSON string instead of an XML structure like:
{"d":"Hello World"}
Does this mean that asp.net web-service implementation returns 'string' types embedded as a value of a property called 'd' ?
Web services don't have to use SOAP as the protocol. The WCF lets you use all sorts of protocols for your web services. SOAP and JSON are just 2 of the options.
ASMX services return JSON if you request them via POST, with a content-type containing application/json. More info here: http://encosia.com/2010/03/03/asmx-and-json-common-mistakes-and-misconceptions/
Their return value isn't limited to just strings either. If you return a collection type, you'll get a JSON array. If you return a server-side class, that will be serialized into the correct JSON key/value pairs to represent that object.
The .d is sort of an orthogonal issue. It's a security feature to thwart a particular attack against JavaScript's Array constructor. More here: http://encosia.com/2009/02/10/a-breaking-change-between-versions-of-aspnet-ajax/
Ok. I have found my answer, here:
"ASP.NET AJAX sends JSON messages to Web Services as opposed to the standard Simple Object Access Protocol (SOAP) calls typically associated with Web Services. This results in smaller request and response messages overall. It also allows for more efficient client-side processing of data since the ASP.NET AJAX JavaScript library is optimized to work with JSON objects. "
More details in :
http://www.asp.net/ajax/tutorials/understanding-asp-net-ajax-web-services
As you can see here JSON responses are supported by recent releases of the .NET Framework.

asp.net not deserializing soap response

I have been given a wsdl and have used wsdl.exe to create my proxy classes.
I am able to call the function to initiate the request with some valid parameters and this returns my response object which is always EMPTY.
When i inspect the soap message response using fiddler the soap does have valid data that should be deserialzed to the proxy classes.
Can i manually intercept the derserializing call of the proxy classes generated by wsdl and check that .net is correctly derializing the soap response?
Thank you
The empty object is most likely the result of a mismatch between the soap message and your proxy class. This can for example be caused by a difference in namespaces (newer version).

Resources