3

WCF Json Service giving an error message 404

 2 years ago
source link: https://www.codesd.com/item/wcf-json-service-giving-an-error-message-404.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

WCF Json Service giving an error message 404

advertisements

BACKGROUND

I have a WCF XML web service and need to convert it to use JSON instead. It is hosted inside a Windows Service (if that matters at all).

PROBLEM

I keep getting a 404 status response.

INTERFACE:

[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Search")]
SearchResponse Search(RequestInfo requestInfo);

CONSOLE APP:

using (var client = new WebClient())
{
    client.Headers["Content-type"] = "application/json";

    var request = new RequestInfo
    {
        //etc
    };

    using (var upStream = new MemoryStream())
    {
        var serializer = new DataContractJsonSerializer(typeof(RequestInfo));
        serializer.WriteObject(upStream, request);

        byte[] responseBytes = client.UploadData("http://localhost:8000/TheService.svc/Search", "POST", upStream.ToArray());

        using (var downStream = new MemoryStream(responseBytes))
        {
            var deserializer = new DataContractJsonSerializer(typeof(Response));
            var result = deserializer.ReadObject(downStream) as Response;
            return result.SearchResult.QueryId;
        }
    }
}

OTHER

I have also tried using Fiddler 2.0 and directly passing in the JSON request like so:

POST http://localhost:8000/TheService.svc/Search HTTP/1.1
User-Agent: Fiddler
Content-Length: 209
Content-Type: application/json; charset=utf-8
Expect: 100-continue
Host: localhost:8000

{my json here}

However, that just confirms the 404. What am I missing here?

UPDATE:

Just to note, I can actually browse to http://localhost:8000/TheService.svc and that works fine. It displays the standard web service page created by WCF.


As it turns out, something in the web.config was causing the problem (<system.serviceModel> section). I just decided to remove everything in that section and start with a bare bones config and then build up from there... now it works.. I did not take the time to try with each and every option I had before, so I don't know exactly which option was causing the problem. If anyone gets this same problem, I advise starting with a basic config and building up from there.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK