【发布时间】:2014-05-30 14:38:09
【问题描述】:
这是我的 WCF 服务合同,方法信号之一:
[WebInvoke(UriTemplate = "", Method = "POST")]
public InventoryResponse Post(InventoryRequest inventoryRequest)
{
当我向那个端点发出 POST 请求时,这个 REST 端点在提琴手中工作,我确实成功地取回了 json:
我要发布到的端点:http://localhost/Inventory/
标题:
但是这并没有,它正在尝试调用相同的 url。我最终在响应中返回了 500 个服务器端错误,说“内部服务器错误”,这可能意味着某些代码失败了,但是提琴手调用是如何工作的......似乎这并没有加起来。
using (var client = new HttpClient())
{
var postUri= "http://localhost/Inventory/";
client.BaseAddress = new Uri(postUri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
inventoryResponse = client.PostAsJsonAsync("", inventoryRequest).Result.Content.ReadAsAsync<InventoryResponse>().Result;
}
此错误表示我认为我没有从服务中获取 json 对象:
System.Net.Http.UnsupportedMediaTypeException: No MediaTypeFormatter is available to read an object of type 'InventoryResponse' from content with media type 'text/html'.
System.AggregateException: One or more errors occurred. ---> System.Runtime.Serialization.SerializationException: Error in line 1 position 70. Expecting element 'InventoryPostResponse' from namespace 'http://schemas.datacontract.org/2004/07/EventInventory.Applications.WebService.Jetson.Responses.BulkPosting'.. Encountered 'Element' with name 'Fault', namespace 'http://schemas.microsoft.com/ws/2005/05/envelope/none'.
【问题讨论】:
-
为什么不使用 Fiddler 检查实际发送到服务器的内容?我猜你只是有一个错误页面作为回报,它有
text/html媒体类型。 -
我刚刚给你看了
-
您显示的是 Fiddler Composer,而不是实际请求。
-
好的,我刚刚发布了请求和响应头信息
-
我认为存在误解。你说你得到了
500 Internal Server Error,但是向我们展示了一个成功的调用(据我所知)使用 Fiddler 本身。为什么不捕获您的HttpClient生成的流量?还是有什么不允许你这样做?
标签: c# .net json wcf asp.net-web-api