How to properly dispose the stream when using StreamContent

As your only resource that needs to be disposed is the Content of the HttpResponseMessage you don’t have to worry about it. The framework does the dispose for you. It will dispose the HttpResponseMessage which will do all the disposing needed. Remove the using and it should work just fine.

The HttpResponseMessage will dispose its Content when it is disposed.
See .NET Core implementation

The StreamContent will dispose its stream when it is disposed.
See .NET Core implementation of StreamContent

If you need to dispose something not disposed by the HttpResponseMessage you may use request.RegisterForDispose as described by Filip Woj

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)