我们正在构建一个使用Owin托管的WebApi。以前我们已经使用HttpResponseException在我们的控制器操作中返回404状态代码等,并且它一直运行良好。
但是,当我们开始使用Owin(自托管)时,我们遇到了这种方法的问题,导致HttpResponseException被序列化为json / xml,状态代码从404更改为500(内部服务器错误)。这是我们的代码:
public class InvoicesController : ApiController
{
private readonly IInvoiceRepository _invoiceRepository;
public InvoicesController(IInvoiceRepository invoiceRepository)
{
_invoiceRepository = invoiceRepository;
}
[HttpGet]
public IEnumerable<AccountCodeAssignment> AssignAccountCodesToInvoiceById(int id)
{
var invoice = _invoiceRepository.Get(id);
if (invoice == null) throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Invoice not found"));
yield return new AccountCodeAssignment(1, ...);
yield return new AccountCodeAssignment(2, ...);
yield return new AccountCodeAssignment(3, ...);
yield return new AccountCodeAssignment(4, ...);
}
}
这是我们得到的响应以及500响应代码:
{
"Message": "An error has occurred.",
"ExceptionMessage": "Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.",
"ExceptionType": "System.Web.Http.HttpResponseException",
"StackTrace": " at AccountCodeAssignmentService.Controllers.InvoicesController.<AssignAccountCodesToInvoiceById>d__0.MoveNext() in c:\\Projects\\AccountCodeAssignmentService\\Source\\AccountCodeAssignmentService\\Controllers\\InvoicesController.cs:line 38\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Owin.HttpMessageHandlerAdapter.<BufferResponseContentAsync>d__13.MoveNext()"
}
使用Owin自托管时,不支持任何关于我们做错了什么或HttpResponseException的想法?
编辑:使用WebApi的一大优势是能够使用并返回我们自己的类型,因此我们希望避免更改返回类型。我们目前正在产生AccountCodeAssignment,因此不能选择更改返回类型。
我不认为问题在于投掷HttpResponseException
。如果查看发布的堆栈跟踪,问题似乎出现在MoveNext()
的调用中。这是您拥有的yield
语句的内部C#表示。
我可能是错的,但验证这个的最简单方法是在第一个yield语句上放置一个断点,看它是否会命中它。我的猜测是它会,即它不会抛出HttpResponseException
。此外,只需暂时更改您的代码,以便始终抛出HttpResponseException
并查看它如何处理它。
我目前正在开发一个使用OWIN自我托管的项目,我可以毫无问题地抛出HttpResponseException
s。
在相关的说明中,您可能想要调查global exception handling。我发现将所有异常处理集中在一个地方非常有用。请注意,HttpResponseException
是一种特殊情况,并不由全局异常处理程序处理。
我在使用邮递员测试web api时遇到了这种情况,请求类型设置为纯文本而不是application / json。
对我来说,我在api标题中缺少内容类型。添加内容类型后,application / json为我解决了这个问题。这可能会帮助别人。
不是100%肯定,但可能会发生,因为你的行为返回IEnumerable <>
尝试将其更改为IHttpActionResult
试试这个
[ResponseType(typeof(AccountCodeAssignment))]
public IHttpActionResult AssignAccountCodesToInvoiceById (int id)
{
var invoice = _invoiceRepository.Get(id);
if (invoice == null) {
return NotFound();
}
return Ok(invoice);
}