title: System.ObjectDisposedException when writing into HttpContext
body: <p>I made custom exception middleware in my rest api project:</p>
  <pre><code>public async Task InvokeAsync(HttpContext httpContext)              
  {                                                                   
      try                                                             
      {                                                               
          await _next(httpContext);                                   
      }                                                                                                                           
      catch (Exception ex)                                            
      {                                                               
          _logger.Error($&quot;{ex}&quot;);               
          await HandleExceptionAsync(httpContext);                    
      }                                                               
  }    
  
  private Task HandleExceptionAsync(HttpContext context)          
  {                                                               
      context.Items.Clear();                                      
      context.Response.StatusCode = StatusCodes.Status200OK;      
      context.Response.ContentType = &quot;application/json&quot;;          
                                                                  
      return context.Response.WriteAsync(&quot;Internal Server Error&quot;);
  }                                                                    
  </code></pre>
  <p>When some exceptions were catched, i get errors:</p>
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
    ?, in Task SaveTempDataFilter.OnStarting(HttpContext httpContext)
    ?, in Task HttpProtocol.FireOnStartingMayAwait(Stack&lt;KeyValuePair&lt;Func&lt;object, Task&gt;, object&gt;&gt; onStarting)
  System.ObjectDisposedException: The response has been aborted due to an unhandled application exception.
    ?, in void HttpProtocol.ThrowResponseAbortedException()
    ?, in Task HttpProtocol.InitializeResponseAsync(int firstWriteByteCount)
    ?, in Task HttpProtocol.Microsoft.AspNetCore.Http.Features.IHttpResponseBodyFeature.StartAsync(CancellationToken cancellationToken)
    ?, in Task DefaultHttpResponse.StartAsync(CancellationToken cancellationToken)
    ?, in Task HttpResponseWritingExtensions.WriteAsync(HttpResponse response, string text, Encoding encoding, CancellationToken cancellationToken) x 2
    File &quot;/Middleware/ExceptionWrapperMiddleware.cs&quot;, line 57, col 13, in Task ExceptionWrapperMiddleware.HandleExceptionAsync(HttpContext context)
    File &quot;/Middleware/ExceptionWrapperMiddleware.cs&quot;, line 50, col 9, in async Task ExceptionWrapperMiddleware.InvokeAsync(HttpContext httpContext)
    File &quot;/_/src/Sentry.AspNetCore/SentryMiddleware.cs&quot;, line 139, col 9, in async Task SentryMiddleware.InvokeAsync(HttpContext context)
  </code></pre>
  <p>My question is, what is wrong with my code that i get Null Reference Exception and ObjectDisposedException in the same time?</p>