Kri*_*Ahl 11 c# asp.net-web-api owin
我们正在构建一个使用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, ...);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我们得到的响应以及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()"
}
Run Code Online (Sandbox Code Playgroud)
使用Owin自托管时,不支持任何关于我们做错了什么或HttpResponseException的想法?
编辑: 为我们使用WebApi的一大优势是能够使用并返回我们自己的类型,因此我们希望避免更改返回类型.我们目前正在产生AccountCodeAssignment,因此不能选择更改返回类型.
我认为问题不在于投掷HttpResponseException
。如果您查看您发布的堆栈跟踪,问题似乎出在对 的调用中MoveNext()
。这是您拥有的语句的内部 C# 表示yield
。
我可能是错的,但验证这一点的最简单方法是在第一个yield语句上放置一个断点,看看它是否命中它。我的猜测是它会,即它不会抛出HttpResponseException
. 另外,只需暂时更改您的代码以始终抛出 anHttpResponseException
并查看它如何处理它。
我目前正在开发一个使用 OWIN 自托管的项目,我可以HttpResponseException
毫无问题地抛出 s 。
与此相关的是,您可能想要研究全局异常处理。我发现将所有异常处理集中在一个地方非常有用。请注意,这HttpResponseException
是一种特殊情况,不由全局异常处理程序处理。
归档时间: |
|
查看次数: |
32098 次 |
最近记录: |