由于某些原因我觉得这个帖子的调用没有正确设置意味着给我的消息我想是在说WCF中的方法没有在POST中接收InventoryRequest对象?
var inventoryRequest = new InventoryRequest
{
memberID = "1",
Inventories = _inventories.ToArray()
};
//Act
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost/Inventory");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
inventoryResponse = await client.PostAsJsonAsync("", inventoryRequest).Result.Content.ReadAsAsync<InventoryResponse>();
}
我在MSTest中收到此错误:
System.Runtime.Serialization.SerializationException: Error in line 1 position 70. Expecting element 'InventoryResponse' from namespace 'http://schemas.datacontract.org/2004/07/Responses'.. Encountered 'Element' with name
'Fault', namespace 'http://schemas.microsoft.com/ws/2005/05/envelope/none'.
at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlReader reader)
这也很奇怪,它正在讨论上面的xml阅读器。我指定json作为内容类型,为什么它试图用xml解析?
旁注:当我尝试在Fiddler中对同一个uri进行POST并在fiddler体中指定InventoryRequest的json表示时,它工作正常......并返回json
答案 0 :(得分:0)
尝试这种方式:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
inventoryResponse = await client.PostAsJsonAsync("Inventory", inventoryRequest).Result.Content.ReadAsAsync<InventoryResponse>();
}