0

背景

我有一个 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 服务页面。

4

2 回答 2

1

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

于 2013-08-30T09:23:41.617 回答
0

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

采用

   UriTemplate = "Search"

代替UriTemplate = "/Search"

尝试 10.0.2.2 而不是 localhost

代替http://localhost:8000/TheService.svc/hello

删除它BodyStyle = WebMessageBodyStyle.Wrapped或改为使用

BodyStyle = WebMessageBodyStyle.Bare
于 2013-08-30T07:24:25.363 回答