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