简体   繁体   中英

WCF Json Service giving 404 error message

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.

It would be great if you show your web.config code as well.

use

   UriTemplate = "Search"

instead of UriTemplate = "/Search"

try 10.0.2.2 instead of localhost

instead of http://localhost:8000/TheService.svc/hello

remove it BodyStyle = WebMessageBodyStyle.Wrapped or instead use

BodyStyle = WebMessageBodyStyle.Bare

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM