title: When to dispose of a HttpResponseMessage or a HttpContent
body: <p>I'm setting up an API to interact with a database and when the api receives a post to create an entry that is already present on it's respective table I want to return a code 209 Conflict with an explanatory message. So I tried this:</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public async Task&lt;IHttpActionResult&gt; PostOrganizationAsync(Organization organization)
  {
      using (var context = new ContextHandler())
      {
          bool added = await context.AddOrganizationAsync(organization);
          if (added)
          {
              return Ok();
          }
          else
          {
              using (var httpContent = new StringContent("An organization with this Id already exists on the database"))
              {
                  return new CustomHttpResponse(HttpStatusCode.Conflict, httpContent);
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>ContextHandler</strong></p>
  
  <pre><code>class ContextHandler : IDisposable
  {
      static AuditLogsEntities LogDatabaseContext;
  
      public ContextHandler()
      {
          LogDatabaseContext = new AuditLogsEntities();
          LogDatabaseContext.Configuration.LazyLoadingEnabled = false;
      }
  
      public void Dispose()
      {
          LogDatabaseContext.Dispose();
      }
  
      public async Task&lt;bool&gt; AddOrganizationAsync(Organization organization)
      {
          var findOrg = await LogDatabaseContext.OrganizationSet.FindAsync(organization.Id);
          if (findOrg != null)
          {
              return false;
          }
          else
          {
              LogDatabaseContext.OrganizationSet.Add(organization);
              await LogDatabaseContext.SaveChangesAsync();
              return true;
          }
      }
  }
  </code></pre>
  
  <p><strong>CustomHttpResponse</strong></p>
  
  <pre><code>public class CustomHttpResponse : IHttpActionResult
      {
          public HttpStatusCode StatusCode;
          public HttpContent Content;
  
          public CustomHttpResponse(HttpStatusCode httpStatusCode, HttpContent httpContent)
          {
              StatusCode = httpStatusCode;
              Content = httpContent;
          }
  
  #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
          public async Task&lt;HttpResponseMessage&gt; ExecuteAsync(CancellationToken cancellationToken)
  #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
          {
              HttpResponseMessage httpResponse = null;
              using (httpResponse = new HttpResponseMessage(StatusCode))
              {
                  httpResponse.Content = Content;
                  return httpResponse;
              }         
          }
      }
  </code></pre>
  
  <p>Posting a new valid entry works fine, but trying to post an already existing one to test the 209 response results in an <code>ObjectDisposedException</code>, which I assume happens because of one of my <code>using</code> blocks, but the issue is I'm not sure which block it is and why it would result in an error.</p>
  
  <p>EDIT: I somehow only now realized that according to the StackTrace, the exception is thrown when the <code>private HttpRequestMessage _request</code> property is being set inside <code>HttpResponseMessage</code></p>