WCF Json服务给出404错误消息

本文关键字:错误 消息 Json 服务 WCF | 更新日期: 2023-09-27 18:29:10

背景

我有一个WCF XML web服务,需要将其转换为使用JSON。它托管在Windows服务中(如果这很重要的话)。

问题

我一直收到404状态的回复。

接口:

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

控制台应用程序:

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;
        }
    }
}

其他

我还尝试过使用Fiddler 2.0并直接传入JSON请求,如下所示:

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}

然而,这只是证实了404。我在这里错过了什么?

更新:

需要注意的是,我实际上可以浏览到http://localhost:8000/TheService.svc,这很好。它显示WCF创建的标准web服务页面。

WCF Json服务给出404错误消息

事实证明,web.config中的某些内容导致了问题(<system.serviceModel>部分)。我只是决定删除该部分中的所有内容,从一个基本配置开始,然后从那里开始构建。。。现在它起作用了。。我没有花时间尝试我以前的每一个选项,所以我不知道到底是哪个选项导致了问题。如果有人遇到同样的问题,我建议从基本配置开始,然后再进行构建。

如果您也显示web.config代码,那就太好了。

使用

   UriTemplate = "Search"

而不是UriTemplate = "/Search"

尝试10.0.2.2而不是localhost

而不是http://localhost:8000/TheService.svc/hello

删除BodyStyle = WebMessageBodyStyle.Wrapped或使用

BodyStyle = WebMessageBodyStyle.Bare