加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:57:10 所属栏目:百科 来源:网络整理
导读:我们正在构建一个使用Owin托管的WebApi.以前我们已经使用HttpResponseException在我们的控制器操作中返回404状态代码等,并且它一直运行良好. 但是,当我们开始使用Owin(自托管)时,我们遇到了这种方法的问题,导致HttpResponseException被序列化为json / xml,状
我们正在构建一个使用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:ProjectsAccountCodeAssignmentServiceSourceAccountCodeAssignmentServiceControllersInvoicesController.cs:line 38rn   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IEnumerable values,JsonArrayContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)rn   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonContainerContract containerContract,JsonProperty containerProperty)rn   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Type objectType)rn   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Type objectType)rn   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type,Stream writeStream,Encoding effectiveEncoding)rn   at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type,Encoding effectiveEncoding)rn   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type,HttpContent content)rn   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type,HttpContent content,TransportContext transportContext,CancellationToken cancellationToken)rn--- End of stack trace from previous location where exception was thrown ---rn   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn   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是一种特殊情况,并不由全局异常处理程序处理.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读