Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF ResponseFormat JSON Returns Json in Fiddler, Xml in Chrome/Firefox!

Tags:

json

c#

wcf

Hi I have a WCF Rest 4.0 project. For some reason I have a webservice which should return Json and it does if i hit the endpoint over fiddler but thru firefox or chrome if I type in the address I get xml. Whats going on???

Thanks for any help! Here's the code.

Web service in question:

  [OperationContract]
  [WebGet(UriTemplate = "",                  
          ResponseFormat = WebMessageFormat.Json,
          RequestFormat = WebMessageFormat.Json)]

        public SomeObject [] GetObjects()
        {
              .....

Object code:

[DataContract]
public class SomeObject
{      
        [DataMember]
        public string Date { get; private set; }

        ....
            public String Site { get; private set; }
like image 534
Luke Belbina Avatar asked Feb 09 '11 07:02

Luke Belbina


2 Answers

I posted this as a comment, but I'll add more detail here.

Your browser is most likely sending this header:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Note that it does not mention json but does mention xml.

Your WCF client is most likely using a different "Accept" header that gives a preference to json. You can check this in Fiddler.

like image 142
Foole Avatar answered Nov 04 '22 02:11

Foole


If you are using the .NET 4.0 framework, this is the solution: http://karnicki.eu/2011/02/rest-wcf-net-4-0-service-with-json-jsonp-for-jquery/

WCF now has JSONP support out of the box with little configuration required.

Basically you just need to edit/add two config file entries, authenticationMode and StandardEndpoint, and voila, you can view json response from your WCF service in any browser.

EDIT: The original link is down - this might help: http://blog.shutupandcode.net/?p=696

like image 29
fjxx Avatar answered Nov 04 '22 01:11

fjxx