c# – 使用Owin自托管时从WebApi控制器抛出HttpResponseException

前端之家收集整理的这篇文章主要介绍了c# – 使用Owin自托管时从WebApi控制器抛出HttpResponseException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们正在构建一个使用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,JsonContainerContract containerContract,JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Type objectType)\r\n   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Type objectType)\r\n   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type,Stream writeStream,Encoding effectiveEncoding)\r\n   at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type,Encoding effectiveEncoding)\r\n   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type,HttpContent content)\r\n   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type,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自托管的项目,我可以毫无问题地抛出HttpResponseExceptions.

在相关的说明中,您可能想要调查global exception handling.我发现将所有异常处理集中在一个地方非常有用.请注意,HttpResponseException是一种特殊情况,并不由全局异常处理程序处理.

原文链接:https://www.f2er.com/csharp/100074.html

猜你在找的C#相关文章