0

How do I get JSON from a self-hosted WCF 4.5 service?

I'm using Fiddler2 to send requests with "Content-Type: application/json" (also tried "Content-Type: application/javascript") but I keep getting XML.

In combination with setting "AutomaticFormatSelectionEnabled = true" on my WebHttpBehavior I still get XML and when using "Content-Type: application/json" the server won't respond at all (Then I get error 103)

I have enabled CrossDomainScriptAccessEnabled on my WebHttpBinding and I'm using a WebServiceHost in a console host.

The service is very simple:

[ServiceContract]
public interface IWebApp
{
  [OperationContract, WebGet(UriTemplate = "/notes/{id}")]
  Note GetNoteById(string id);
}

I've also tried setting AutomaticFormatSelectionEnabled to false and using ResponseFormat = WebMessageFormat.Json in my service-contract but that also result in "error 103" with no further info.

I've turned of customErrors and set FaultExceptionEnabled, HelpEnabled to true (not sure if that would do anything about this but just to make sure I've tried it all)

Am I missing a dll or something else?

Andreas Zita
  • 7,232
  • 6
  • 54
  • 115

1 Answers1

5

Try to start simple, as in the code below (which works on 4.5). From there, you can start adding the features your code uses one at a time, until you find the moment it breaks. That will give you a better idea what is going wrong.

using System;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            string baseAddress = "http://localhost:8000/Service";
            WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
            host.Open();
            Console.WriteLine("Host opened");

            WebClient c = new WebClient();
            Console.WriteLine(c.DownloadString(baseAddress + "/notes/a1b2"));

            Console.WriteLine("Press ENTER to close");
            Console.ReadLine();
            host.Close();
        }
    }

    public class Note
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public string Contents { get; set; }
    }

    [ServiceContract]
    public interface IWebApp
    {
        [OperationContract, WebGet(UriTemplate = "/notes/{id}", ResponseFormat = WebMessageFormat.Json)]
        Note GetNoteById(string id);
    }

    public class Service : IWebApp
    {
        public Note GetNoteById(string id)
        {
            return new Note
            {
                Id = id,
                Title = "Shopping list",
                Contents = "Buy milk, bread, eggs, fruits"
            };
        }
    }
}
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • 1
    Thanks for the push. Your example worked fine so I tried to make it more and more like my own to see where it broke. And I found out that the problem was a DateTime-property in my Note-class that was set to DateTime.MinValue and this was "obviously" not supported by the serializer. What a headake to debug! Finally got it =) – Andreas Zita Mar 21 '13 at 16:59
  • I have same problems with DateTime. Thanks a lot, this post save my tyme! – Adam D Dec 23 '13 at 09:19