问题描述
我使用 asp.net 3.1 和 Swagger (Swashbuckle.AspNetCore) 5.4.1 来生成 api 文档。我有一个带有 DELETE
方法的 api,它删除资源并返回 NoContent(204)
响应,如下所示:
[HttpDelete("{id:guid}")]
public async Task<ActionResult> DeleteAsync(Guid id)
{
var existedEntity = await _discountCodePolicyRepository.GetAsync(id,HttpContext.RequestAborted);
if (existedEntity == null)
return NotFound();
await _discountCodePolicyRepository.DeleteAsync(existedEntity,HttpContext.RequestAborted);
return NoContent();
}
它在 swagger UI 中运行良好,但是当从我们的客户(网站)调用时,抛出异常:
system.invalidOperationException: Writing to the response body is invalid for responses with status code 204. Module "Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol",in ThrowWritingToResponseBodyNotSupported Module "Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol",in FirstWriteAsyncInternal Module "Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol",in FirstWriteAsync Module "Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol",in WritePipeAsync Module "Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream",in WriteAsync Module "Microsoft.AspNetCore.WebUtilities.PagedByteBuffer",in MovetoAsync Module "System.Runtime.ExceptionServices.ExceptiondispatchInfo",in Throw Module "System.Runtime.CompilerServices.TaskAwaiter",in ThrowForNonSuccess Module "System.Runtime.CompilerServices.TaskAwaiter",in HandleNonSuccessAndDebuggerNotification Module "Microsoft.AspNetCore.WebUtilities.filebufferingWriteStream",in DrainBufferAsync Module "System.Runtime.ExceptionServices.ExceptiondispatchInfo",in HandleNonSuccessAndDebuggerNotification Module "Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter",in WriteResponseBodyAsync Module "System.Runtime.ExceptionServices.ExceptiondispatchInfo",in Throw Module "Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter",in WriteResponseBodyAsync Module "System.Runtime.ExceptionServices.ExceptiondispatchInfo",in HandleNonSuccessAndDebuggerNotification Module "Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker+ <<InvokeResultFilters>g__Awaited|27_0>d",in MoveNext Module "System.Runtime.ExceptionServices.ExceptiondispatchInfo",in HandleNonSuccessAndDebuggerNotification Module "Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker+ <<InvokeFilterPipelineAsync>g__Awaited|19_0>d",in HandleNonSuccessAndDebuggerNotification Module "Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker+<<InvokeAsync>g__Awaited|17_0>d",in MoveNext Module "System.Runtime.ExceptionServices.ExceptiondispatchInfo",in HandleNonSuccessAndDebuggerNotification Module "Microsoft.AspNetCore.Routing.EndpointMiddleware+<<Invoke>g__AwaitRequestTask|6_0>d",in HandleNonSuccessAndDebuggerNotification Module "Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware",in Invoke Module "System.Runtime.ExceptionServices.ExceptiondispatchInfo",in HandleNonSuccessAndDebuggerNotification Module "Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware",in HandleNonSuccessAndDebuggerNotification Module "Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+ <ProcessRequests>d__214`1",in MoveNext
我用谷歌搜索了这个,但我没有发现任何有用的东西。
更新:
检查更多细节后。这个项目是一个用于管理折扣代码的微服务。在微服务中,我从其 swaggerUI 中成功删除了折扣代码并获得 204 状态代码。但是当从 ApiGateway swaggerUI 调用时,抛出异常并带有消息:
system.invalidOperationException: Response Content-Length mismatch: too few bytes written (0 of 23).
我认为这个异常会导致微服务异常,因为管道无法完成。
我检查了 api 网关,在调用下游后没有中间件来添加内容我使用 Ocelate
作为 api 网关。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)