我有一个简单的宁静服务,看起来像这样。
[OperationContract]
[WebInvoke(Method = "DELETE",
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "cars/{id}")]
Car DeleteCar(string id);
public class CarService : ICarService
{
private static List<Car> cList = new List<Car>()
{
new Car("VW","Polo",2010,200000,"VWP123"),
new Car("Volvo","V70",2010,280000,"VOL123"),
new Car("BMW","Z4",2014,400000,"BMW123")
};
public Car DeleteCar(string id)
{
Car c = GetCar(id);
if (c == null) return null;
cList.Remove(c);
return c;
}
}
我尝试使用控制台应用程序来使用它,它有效。它会从列表中删除汽车,但控制台应用程序崩溃。
carNo = Console.ReadLine().ToUpper();
Console.WriteLine(DeleteCar(carNo));
private static Car DeleteCar(string frameNo)
{
Car car = DeleteCarAsync(frameNo).Result;
return car;
}
private static async Task<Car> DeleteCarAsync(string frameNo)
{
string reqUri = baseUri + frameNo;
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.DeleteAsync(reqUri);
if (response.StatusCode == HttpStatusCode.NotFound)
throw new Exception("Car not found.");
response.EnsureSuccessStatusCode();
string str = await response.Content.ReadAsStringAsync();
Car car = JsonConvert.DeserializeObject<Car>(str);
return car;
}
}
运行代码时,控制台崩溃为&#34;未处理的类型&#39; System.AggregateException&#39;发生在mscorlib.dll&#34;在这&#34; Car car = DeleteCarAsync(frameNo).Result;&#34; peticaler线。当我重新启动控制台时,打印列表并且我要删除的汽车已经消失。
任何见解都会得到满足。
修改 继承你要求的内在例外: Inner Exceptions
根据我从错误中理解的内容,它不会让json字符串返回。