1+ using Newtonsoft . Json ;
12using System ;
23using System . IO ;
34using System . Net . Http ;
45using System . Text ;
56using System . Threading ;
67using System . Threading . Tasks ;
7- using Newtonsoft . Json ;
88
99namespace Docker . DotNet . Models
1010{
@@ -17,15 +17,12 @@ internal static async Task MonitorStreamAsync(Task<Stream> streamTask, DockerCli
1717 using ( var stream = await streamTask )
1818 {
1919 // ReadLineAsync must be cancelled by closing the whole stream.
20- using ( cancel . Register ( ( ) => stream . Dispose ( ) ) )
20+ using ( var reader = new StreamReader ( stream , new UTF8Encoding ( false ) ) )
2121 {
22- using ( var reader = new StreamReader ( stream , new UTF8Encoding ( false ) ) )
22+ string line ;
23+ while ( ( line = await reader . ReadLineAsync ( ) ) != null && ! cancel . IsCancellationRequested )
2324 {
24- string line ;
25- while ( ( line = await reader . ReadLineAsync ( ) ) != null )
26- {
27- progress . Report ( line ) ;
28- }
25+ progress . Report ( line ) ;
2926 }
3027 }
3128 }
@@ -49,32 +46,29 @@ internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseM
4946 {
5047 using ( var response = await responseTask )
5148 {
52- await client . HandleIfErrorResponseAsync ( response . StatusCode , response ) ;
53-
5449 using ( var stream = await response . Content . ReadAsStreamAsync ( ) )
5550 {
5651 // ReadLineAsync must be cancelled by closing the whole stream.
57- using ( cancel . Register ( ( ) => stream . Dispose ( ) ) )
52+ using ( var reader = new StreamReader ( stream , new UTF8Encoding ( false ) ) )
5853 {
59- using ( var reader = new StreamReader ( stream , new UTF8Encoding ( false ) ) )
54+ string line ;
55+ try
6056 {
61- string line ;
62- try
57+ while ( ( line = await reader . ReadLineAsync ( ) ) != null && ! cancel . IsCancellationRequested )
6358 {
64- while ( ( line = await reader . ReadLineAsync ( ) ) != null )
65- {
66- var prog = client . JsonSerializer . DeserializeObject < T > ( line ) ;
67- if ( prog == null ) continue ;
59+ await client . HandleIfErrorResponseAsync ( response . StatusCode , response ) ;
6860
69- progress . Report ( prog ) ;
70- }
71- }
72- catch ( ObjectDisposedException )
73- {
74- // The subsequent call to reader.ReadLineAsync() after cancellation
75- // will fail because we disposed the stream. Just ignore here.
61+ var prog = client . JsonSerializer . DeserializeObject < T > ( line ) ;
62+ if ( prog == null ) continue ;
63+
64+ progress . Report ( prog ) ;
7665 }
7766 }
67+ catch ( ObjectDisposedException )
68+ {
69+ // The subsequent call to reader.ReadLineAsync() after cancellation
70+ // will fail because we disposed the stream. Just ignore here.
71+ }
7872 }
7973 }
8074 }
0 commit comments