0

I have an action that returns an HttpResponseMessage.

How do I dispose such an object? After I return it, I can't really dispose it.

Example:

 return new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(json, Encoding.UTF8, "application/json") };
 // Inside function SomeMethod

My Action:

 public HttpResponseMessage SomeAction()
 {
    return SomeMethod(); // Warning that this is not disposed
 }

It's weird because on other parts of my code no such warning exists. Is the function confusing my IntelliSense ?
The action is not invoked from anywhere else.
It returns its result to its endpoint.

LopDev
  • 823
  • 10
  • 26
SpiritBob
  • 2,355
  • 3
  • 24
  • 62
  • After it gets returned, and isn't referenced, it should be disposed/marked for collection already, right? – Zachary Brooks Oct 30 '19 at 13:59
  • @ZacharyBrooks - There is an explicit Dispose, from the IDisposable interface that HttpResponseMessage implements. – Rakesh Oct 30 '19 at 14:02
  • Possible duplicate of [When or if to Dispose HttpResponseMessage when calling ReadAsStreamAsync?](https://stackoverflow.com/questions/27715327/when-or-if-to-dispose-httpresponsemessage-when-calling-readasstreamasync) – William Xifaras Oct 30 '19 at 14:03
  • @SpiritBob - You would dispose of it at the consumer end - or am I missing something. – Rakesh Oct 30 '19 at 14:04
  • @Rakesh No, I'll provide an example. – SpiritBob Oct 30 '19 at 14:06
  • What is warning you? Is it some extension you've installed in your IDE? – mason Oct 30 '19 at 14:12
  • @mason Intelisense? I don't have anything else installed like JetBrains etc – SpiritBob Oct 30 '19 at 14:13
  • It would seem that the most obvious way to get out of this predicament would be to return the actual content as opposed to the HttpResponseMessage, but maybe the community will have some thoughts about best practices. – Rakesh Oct 30 '19 at 15:24

1 Answers1

0

After researching, the best practice according to this article is to dispose it.

It states that:

The safest, general advice would be to always dispose of the HttpResponseMessage once you have finished with using it. This does lead to a little more code noise but ensures that regardless of the internals and any future changes, your code will free/clean up unused resources such as connections as quickly as possible. Be aware that once you dispose of the content, it will become unusable for anyone else who has been passed a reference to it.

The article provides this example on how to properly use the HttpResponseMessage and then dispose of it

public async Task<SomeData> GetSomeDataAsync(Uri uri)
{

    HttpResponseMessage response = await _httpClient.GetAsync(uri);
    SomeData result = null; 

    try
    {
        // In this case we'll expect our caller to handle a HttpRequestException
        // if this request was not successful.
        response.EnsureSuccessStatusCode();

        result = await response.Content?.ReadAsAsync<SomeData>();
    }
    finally
    {
        // Dispose of HttpResponseMessage to ensure we free up system resources 
        // and release the network connection (if that hasn't already happened).
        // Not required in a majority of common cases but always safe to do as long
       // as you have not passed the content onto other threads / consumers.
        response.Dispose(); 
    }

    // Either return the result or in this case a daft 
    // default value. It's okay for this example!
    return result ?? SomeData.Empty; 
}
Zachary Brooks
  • 303
  • 2
  • 9
  • Did you see my example? I'm unable to achieve what you've shown me, because for me to dispose it, I have to be back into context, which is impossible. – SpiritBob Oct 30 '19 at 14:47
  • 3
    That’s not the best practice. Best practice for classes implementing IDisposable is to use `using`. You would call Dispose directly only when `using` isn’t possible, e.g. when declaration and disposable are in different methods/scopes. – ckuri Oct 30 '19 at 20:18
  • @SpiritBob The answer, as is visible in my example, is stating that you should return the content versus the object, so you can dispose of the response. – Zachary Brooks Nov 05 '19 at 21:08