@@ -90,22 +90,12 @@ public static Task WriteAsJsonAsync<TValue>(
9090
9191        response . ContentType  =  contentType  ??  ContentTypeConstants . JsonContentTypeWithCharset ; 
9292
93-         var  startTask  =  Task . CompletedTask ; 
94-         if  ( ! response . HasStarted ) 
95-         { 
96-             // Flush headers before starting Json serialization. This avoids an extra layer of buffering before the first flush. 
97-             startTask  =  response . StartAsync ( cancellationToken ) ; 
98-         } 
99- 
10093        // if no user provided token, pass the RequestAborted token and ignore OperationCanceledException 
101-         if  ( ! startTask . IsCompleted   ||   ! cancellationToken . CanBeCanceled ) 
94+         if  ( ! cancellationToken . CanBeCanceled ) 
10295        { 
103-             return  WriteAsJsonAsyncSlow ( startTask ,  response . BodyWriter ,  value ,  options , 
104-                 ignoreOCE :  ! cancellationToken . CanBeCanceled , 
105-                 cancellationToken . CanBeCanceled  ?  cancellationToken  :  response . HttpContext . RequestAborted ) ; 
96+             return  WriteAsJsonAsyncSlow ( response . BodyWriter ,  value ,  options ,  response . HttpContext . RequestAborted ) ; 
10697        } 
10798
108-         startTask . GetAwaiter ( ) . GetResult ( ) ; 
10999        return  JsonSerializer . SerializeAsync ( response . BodyWriter ,  value ,  options ,  cancellationToken ) ; 
110100    } 
111101
@@ -131,33 +121,22 @@ public static Task WriteAsJsonAsync<TValue>(
131121
132122        response . ContentType  =  contentType  ??  ContentTypeConstants . JsonContentTypeWithCharset ; 
133123
134-         var  startTask  =  Task . CompletedTask ; 
135-         if  ( ! response . HasStarted ) 
136-         { 
137-             // Flush headers before starting Json serialization. This avoids an extra layer of buffering before the first flush. 
138-             startTask  =  response . StartAsync ( cancellationToken ) ; 
139-         } 
140- 
141124        // if no user provided token, pass the RequestAborted token and ignore OperationCanceledException 
142-         if  ( ! startTask . IsCompleted   ||   ! cancellationToken . CanBeCanceled ) 
125+         if  ( ! cancellationToken . CanBeCanceled ) 
143126        { 
144-             return  WriteAsJsonAsyncSlow ( startTask ,  response ,  value ,  jsonTypeInfo , 
145-                 ignoreOCE :  ! cancellationToken . CanBeCanceled , 
146-                 cancellationToken . CanBeCanceled  ?  cancellationToken  :  response . HttpContext . RequestAborted ) ; 
127+             return  WriteAsJsonAsyncSlow ( response ,  value ,  jsonTypeInfo ,  response . HttpContext . RequestAborted ) ; 
147128        } 
148129
149-         startTask . GetAwaiter ( ) . GetResult ( ) ; 
150130        return  JsonSerializer . SerializeAsync ( response . BodyWriter ,  value ,  jsonTypeInfo ,  cancellationToken ) ; 
151131
152-         static async  Task  WriteAsJsonAsyncSlow ( Task   startTask ,   HttpResponse  response ,  TValue  value ,  JsonTypeInfo < TValue >  jsonTypeInfo , 
153-             bool   ignoreOCE ,   CancellationToken  cancellationToken ) 
132+         static async  Task  WriteAsJsonAsyncSlow ( HttpResponse  response ,  TValue  value ,  JsonTypeInfo < TValue >  jsonTypeInfo , 
133+             CancellationToken  cancellationToken ) 
154134        { 
155135            try 
156136            { 
157-                 await  startTask ; 
158137                await  JsonSerializer . SerializeAsync ( response . BodyWriter ,  value ,  jsonTypeInfo ,  cancellationToken ) ; 
159138            } 
160-             catch  ( OperationCanceledException )  when   ( ignoreOCE )   {  } 
139+             catch  ( OperationCanceledException )  {  } 
161140        } 
162141    } 
163142
@@ -184,52 +163,38 @@ public static Task WriteAsJsonAsync(
184163
185164        response . ContentType  =  contentType  ??  ContentTypeConstants . JsonContentTypeWithCharset ; 
186165
187-         var  startTask  =  Task . CompletedTask ; 
188-         if  ( ! response . HasStarted ) 
189-         { 
190-             // Flush headers before starting Json serialization. This avoids an extra layer of buffering before the first flush. 
191-             startTask  =  response . StartAsync ( cancellationToken ) ; 
192-         } 
193- 
194166        // if no user provided token, pass the RequestAborted token and ignore OperationCanceledException 
195-         if  ( ! startTask . IsCompleted   ||   ! cancellationToken . CanBeCanceled ) 
167+         if  ( ! cancellationToken . CanBeCanceled ) 
196168        { 
197-             return  WriteAsJsonAsyncSlow ( startTask ,  response ,  value ,  jsonTypeInfo , 
198-                 ignoreOCE :  ! cancellationToken . CanBeCanceled , 
199-                 cancellationToken . CanBeCanceled  ?  cancellationToken  :  response . HttpContext . RequestAborted ) ; 
169+             return  WriteAsJsonAsyncSlow ( response ,  value ,  jsonTypeInfo ,  response . HttpContext . RequestAborted ) ; 
200170        } 
201171
202-         startTask . GetAwaiter ( ) . GetResult ( ) ; 
203172        return  JsonSerializer . SerializeAsync ( response . BodyWriter ,  value ,  jsonTypeInfo ,  cancellationToken ) ; 
204173
205-         static async  Task  WriteAsJsonAsyncSlow ( Task   startTask ,   HttpResponse  response ,  object ?  value ,  JsonTypeInfo  jsonTypeInfo , 
206-             bool   ignoreOCE ,   CancellationToken  cancellationToken ) 
174+         static async  Task  WriteAsJsonAsyncSlow ( HttpResponse  response ,  object ?  value ,  JsonTypeInfo  jsonTypeInfo , 
175+             CancellationToken  cancellationToken ) 
207176        { 
208177            try 
209178            { 
210-                 await  startTask ; 
211179                await  JsonSerializer . SerializeAsync ( response . BodyWriter ,  value ,  jsonTypeInfo ,  cancellationToken ) ; 
212180            } 
213-             catch  ( OperationCanceledException )  when   ( ignoreOCE )   {  } 
181+             catch  ( OperationCanceledException )  {  } 
214182        } 
215183    } 
216184
217185    [ RequiresUnreferencedCode ( RequiresUnreferencedCodeMessage ) ] 
218186    [ RequiresDynamicCode ( RequiresDynamicCodeMessage ) ] 
219187    private  static async  Task  WriteAsJsonAsyncSlow < TValue > ( 
220-         Task  startTask , 
221188        PipeWriter  body , 
222189        TValue  value , 
223190        JsonSerializerOptions ?  options , 
224-         bool  ignoreOCE , 
225191        CancellationToken  cancellationToken ) 
226192    { 
227193        try 
228194        { 
229-             await  startTask ; 
230195            await  JsonSerializer . SerializeAsync ( body ,  value ,  options ,  cancellationToken ) ; 
231196        } 
232-         catch  ( OperationCanceledException )  when   ( ignoreOCE )   {  } 
197+         catch  ( OperationCanceledException )  {  } 
233198    } 
234199
235200    /// <summary> 
@@ -304,42 +269,30 @@ public static Task WriteAsJsonAsync(
304269
305270        response . ContentType  =  contentType  ??  ContentTypeConstants . JsonContentTypeWithCharset ; 
306271
307-         var  startTask  =  Task . CompletedTask ; 
308-         if  ( ! response . HasStarted ) 
309-         { 
310-             // Flush headers before starting Json serialization. This avoids an extra layer of buffering before the first flush. 
311-             startTask  =  response . StartAsync ( cancellationToken ) ; 
312-         } 
313- 
314272        // if no user provided token, pass the RequestAborted token and ignore OperationCanceledException 
315-         if  ( ! startTask . IsCompleted   ||   ! cancellationToken . CanBeCanceled ) 
273+         if  ( ! cancellationToken . CanBeCanceled ) 
316274        { 
317-             return  WriteAsJsonAsyncSlow ( startTask ,  response . BodyWriter ,  value ,  type ,  options , 
318-                 ignoreOCE :  ! cancellationToken . CanBeCanceled , 
319-                 cancellationToken . CanBeCanceled  ?  cancellationToken  :  response . HttpContext . RequestAborted ) ; 
275+             return  WriteAsJsonAsyncSlow ( response . BodyWriter ,  value ,  type ,  options , 
276+                 response . HttpContext . RequestAborted ) ; 
320277        } 
321278
322-         startTask . GetAwaiter ( ) . GetResult ( ) ; 
323279        return  JsonSerializer . SerializeAsync ( response . BodyWriter ,  value ,  type ,  options ,  cancellationToken ) ; 
324280    } 
325281
326282    [ RequiresUnreferencedCode ( RequiresUnreferencedCodeMessage ) ] 
327283    [ RequiresDynamicCode ( RequiresDynamicCodeMessage ) ] 
328284    private  static async  Task  WriteAsJsonAsyncSlow ( 
329-         Task  startTask , 
330285        PipeWriter  body , 
331286        object ?  value , 
332287        Type  type , 
333288        JsonSerializerOptions ?  options , 
334-         bool  ignoreOCE , 
335289        CancellationToken  cancellationToken ) 
336290    { 
337291        try 
338292        { 
339-             await  startTask ; 
340293            await  JsonSerializer . SerializeAsync ( body ,  value ,  type ,  options ,  cancellationToken ) ; 
341294        } 
342-         catch  ( OperationCanceledException )  when   ( ignoreOCE )   {  } 
295+         catch  ( OperationCanceledException )  {  } 
343296    } 
344297
345298    /// <summary> 
@@ -367,33 +320,22 @@ public static Task WriteAsJsonAsync(
367320
368321        response . ContentType  =  contentType  ??  ContentTypeConstants . JsonContentTypeWithCharset ; 
369322
370-         var  startTask  =  Task . CompletedTask ; 
371-         if  ( ! response . HasStarted ) 
372-         { 
373-             // Flush headers before starting Json serialization. This avoids an extra layer of buffering before the first flush. 
374-             startTask  =  response . StartAsync ( cancellationToken ) ; 
375-         } 
376- 
377323        // if no user provided token, pass the RequestAborted token and ignore OperationCanceledException 
378-         if  ( ! startTask . IsCompleted   ||   ! cancellationToken . CanBeCanceled ) 
324+         if  ( ! cancellationToken . CanBeCanceled ) 
379325        { 
380-             return  WriteAsJsonAsyncSlow ( startTask ,  response . BodyWriter ,  value ,  type ,  context , 
381-                 ignoreOCE :  ! cancellationToken . CanBeCanceled , 
382-                 cancellationToken . CanBeCanceled  ?  cancellationToken  :  response . HttpContext . RequestAborted ) ; 
326+             return  WriteAsJsonAsyncSlow ( response . BodyWriter ,  value ,  type ,  context ,  response . HttpContext . RequestAborted ) ; 
383327        } 
384328
385-         startTask . GetAwaiter ( ) . GetResult ( ) ; 
386329        return  JsonSerializer . SerializeAsync ( response . BodyWriter ,  value ,  type ,  context ,  cancellationToken ) ; 
387330
388-         static async  Task  WriteAsJsonAsyncSlow ( Task   startTask ,   PipeWriter  body ,  object ?  value ,  Type  type ,  JsonSerializerContext  context , 
389-             bool   ignoreOCE ,   CancellationToken  cancellationToken ) 
331+         static async  Task  WriteAsJsonAsyncSlow ( PipeWriter  body ,  object ?  value ,  Type  type ,  JsonSerializerContext  context , 
332+             CancellationToken  cancellationToken ) 
390333        { 
391334            try 
392335            { 
393-                 await  startTask ; 
394336                await  JsonSerializer . SerializeAsync ( body ,  value ,  type ,  context ,  cancellationToken ) ; 
395337            } 
396-             catch  ( OperationCanceledException )  when   ( ignoreOCE )   {  } 
338+             catch  ( OperationCanceledException )  {  } 
397339        } 
398340    } 
399341
0 commit comments