0

I can't seem to succeed with getting JSON out of a WCF service even if I tag the method with the attributes:

[WebGet(UriTemplate = "Product/{productIdString}",
ResponseFormat = WebMessageFormat.Json)]

OR

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

I'm always getting XML, whether I return it as a DataSet or a List<>.

The only way that worked was to manually returning JSON as a string but it was also encapsulated in XML.

Any clue?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bassel Alkhateeb
  • 1,584
  • 5
  • 19
  • 35
  • What **binding** are you using on your service?? The JSON stuff works pretty much only on the `webHttpBinding` (REST-Style WCF Services) – marc_s Oct 30 '11 at 15:35

2 Answers2

2

try something like this:

[WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "products")]
    public IList<JxProduct> GetProductList()
    {
        List<JxProduct> products = new List<JxProduct>();
        products.Add(new JxProduct { Description = "Tire", Id = 1, Price = 39.99});
        products.Add(new JxProduct { Description = "Tube", Id = 2, Price = 4.99 });
        products.Add(new JxProduct { Description = "Patch", Id = 3, Price = 3.99});

        return products;
    }

You can also review the following post which goes into more details about web.config settings. How do I return clean JSON from a WCF Service?

Community
  • 1
  • 1
0

[WebGet] (and [WebInvoke]) attributes are only recognized by the WebHttpBehavior (<webHttp/> if you're using config). Make sure that the endpoint you're hitting has that behavior set.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171