Feedback on ASP.NET Web API

Deal with the fact that finalization of StreamWriter can close the response stream prematurely

A typical pattern is to create a StreamWriter from the Stream argument that is passed to OnWriteToStreamAsync in a media type formatter. The guidance is to *not* close this StreamWriter since doing so will also close the underlying stream (which you don't want).

But what happens if finalization of the once-out-of-scope StreamWriter occurs? Well, it will close the underlying stream!

What is the team's guidance for dealing with this? Simply suppress finalization of the StreamWriter?

18 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Andrew Webb shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →
    completed  ·  Daniel Roth responded  · 

    The recommended pattern is to use the RegisterForDispose extension method on the request message to register all resources that you want disposed when the request is disposed.

    3 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base