title: asp.net core 3.1 Writing to the response body is invalid for responses with status code 204
body: <p>I'm using asp.net 3.1 and Swagger (Swashbuckle.AspNetCore) 5.4.1 to generate api documents. I have an api with <code>DELETE</code> method that deletes a resource and return <code>NoContent(204)</code> response like this:</p>
  <pre><code>    [HttpDelete(&quot;{id:guid}&quot;)]
      public async Task&lt;ActionResult&gt; DeleteAsync(Guid id)
      {
          var existedEntity = await _discountCodePolicyRepository.GetAsync(id, HttpContext.RequestAborted);
  
          if (existedEntity == null)
              return NotFound();
  
          await _discountCodePolicyRepository.DeleteAsync(existedEntity, HttpContext.RequestAborted);
  
          return NoContent();
      }
  </code></pre>
  <p>It's works fine from swagger UI but when called from our clients (website), throw blow exception:</p>
  <p><code>System.InvalidOperationException: Writing to the response body is invalid for responses with status code 204. Module &quot;Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol&quot;, in  ThrowWritingToResponseBodyNotSupported Module &quot;Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol&quot;, in  FirstWriteAsyncInternal Module &quot;Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol&quot;, in FirstWriteAsync Module &quot;Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol&quot;, in WritePipeAsync Module &quot;Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream&quot;, in WriteAsync Module &quot;Microsoft.AspNetCore.WebUtilities.PagedByteBuffer&quot;, in MoveToAsync Module &quot;System.Runtime.ExceptionServices.ExceptionDispatchInfo&quot;, in Throw Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in ThrowForNonSuccess Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in HandleNonSuccessAndDebuggerNotification Module &quot;Microsoft.AspNetCore.WebUtilities.FileBufferingWriteStream&quot;, in DrainBufferAsync Module &quot;System.Runtime.ExceptionServices.ExceptionDispatchInfo&quot;, in Throw Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in ThrowForNonSuccess Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in HandleNonSuccessAndDebuggerNotification Module &quot;Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter&quot;, in  WriteResponseBodyAsync Module &quot;System.Runtime.ExceptionServices.ExceptionDispatchInfo&quot;, in Throw Module &quot;Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter&quot;, in WriteResponseBodyAsync Module &quot;System.Runtime.ExceptionServices.ExceptionDispatchInfo&quot;, in Throw Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in ThrowForNonSuccess Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in HandleNonSuccessAndDebuggerNotification Module &quot;Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker+  &lt;&lt;InvokeResultFilters&gt;g__Awaited|27_0&gt;d&quot;, in MoveNext Module &quot;System.Runtime.ExceptionServices.ExceptionDispatchInfo&quot;, in Throw Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in ThrowForNonSuccess Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in HandleNonSuccessAndDebuggerNotification Module &quot;Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker+  &lt;&lt;InvokeFilterPipelineAsync&gt;g__Awaited|19_0&gt;d&quot;, in MoveNext Module &quot;System.Runtime.ExceptionServices.ExceptionDispatchInfo&quot;, in Throw Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in ThrowForNonSuccess Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in HandleNonSuccessAndDebuggerNotification Module &quot;Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker+&lt;&lt;InvokeAsync&gt;g__Awaited|17_0&gt;d&quot;, in  MoveNext Module &quot;System.Runtime.ExceptionServices.ExceptionDispatchInfo&quot;, in Throw Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in ThrowForNonSuccess Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in HandleNonSuccessAndDebuggerNotification Module &quot;Microsoft.AspNetCore.Routing.EndpointMiddleware+&lt;&lt;Invoke&gt;g__AwaitRequestTask|6_0&gt;d&quot;, in  MoveNext Module &quot;System.Runtime.ExceptionServices.ExceptionDispatchInfo&quot;, in Throw Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in ThrowForNonSuccess Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in HandleNonSuccessAndDebuggerNotification Module &quot;Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware&quot;, in Invoke Module &quot;System.Runtime.ExceptionServices.ExceptionDispatchInfo&quot;, in Throw Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in ThrowForNonSuccess Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in HandleNonSuccessAndDebuggerNotification Module &quot;Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware&quot;, in Invoke Module &quot;System.Runtime.ExceptionServices.ExceptionDispatchInfo&quot;, in Throw Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in ThrowForNonSuccess Module &quot;System.Runtime.CompilerServices.TaskAwaiter&quot;, in HandleNonSuccessAndDebuggerNotification Module &quot;Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+  &lt;ProcessRequests&gt;d__214`1&quot;, in MoveNext</code></p>
  <p>I google this but I've not found anything helpful.</p>
  <p>UPDATE:</p>
  <p>After check more details. this project is a micro service for manage discount codes. in micro service I delete discount code successfully from its swaggerUI and get 204 status code. but when call from ApiGateway swaggerUI, throw exception with message:</p>
  <p><code>System.InvalidOperationException: Response Content-Length mismatch: too few bytes written (0 of 23).</code>
  I think this exception cause to micro service exception because the pipeline can't complete.
  I check the api gateway and there is no middleware to add content after call downstreams I'm using <code>Ocelate</code> for api gateway.</p>