@@ -90,22 +90,12 @@ public static Task WriteAsJsonAsync<TValue>(
90
90
91
91
response . ContentType = contentType ?? ContentTypeConstants . JsonContentTypeWithCharset ;
92
92
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
-
100
93
// if no user provided token, pass the RequestAborted token and ignore OperationCanceledException
101
- if ( ! startTask . IsCompleted || ! cancellationToken . CanBeCanceled )
94
+ if ( ! cancellationToken . CanBeCanceled )
102
95
{
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 ) ;
106
97
}
107
98
108
- startTask . GetAwaiter ( ) . GetResult ( ) ;
109
99
return JsonSerializer . SerializeAsync ( response . BodyWriter , value , options , cancellationToken ) ;
110
100
}
111
101
@@ -131,33 +121,22 @@ public static Task WriteAsJsonAsync<TValue>(
131
121
132
122
response . ContentType = contentType ?? ContentTypeConstants . JsonContentTypeWithCharset ;
133
123
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
-
141
124
// if no user provided token, pass the RequestAborted token and ignore OperationCanceledException
142
- if ( ! startTask . IsCompleted || ! cancellationToken . CanBeCanceled )
125
+ if ( ! cancellationToken . CanBeCanceled )
143
126
{
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 ) ;
147
128
}
148
129
149
- startTask . GetAwaiter ( ) . GetResult ( ) ;
150
130
return JsonSerializer . SerializeAsync ( response . BodyWriter , value , jsonTypeInfo , cancellationToken ) ;
151
131
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 )
154
134
{
155
135
try
156
136
{
157
- await startTask ;
158
137
await JsonSerializer . SerializeAsync ( response . BodyWriter , value , jsonTypeInfo , cancellationToken ) ;
159
138
}
160
- catch ( OperationCanceledException ) when ( ignoreOCE ) { }
139
+ catch ( OperationCanceledException ) { }
161
140
}
162
141
}
163
142
@@ -184,52 +163,38 @@ public static Task WriteAsJsonAsync(
184
163
185
164
response . ContentType = contentType ?? ContentTypeConstants . JsonContentTypeWithCharset ;
186
165
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
-
194
166
// if no user provided token, pass the RequestAborted token and ignore OperationCanceledException
195
- if ( ! startTask . IsCompleted || ! cancellationToken . CanBeCanceled )
167
+ if ( ! cancellationToken . CanBeCanceled )
196
168
{
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 ) ;
200
170
}
201
171
202
- startTask . GetAwaiter ( ) . GetResult ( ) ;
203
172
return JsonSerializer . SerializeAsync ( response . BodyWriter , value , jsonTypeInfo , cancellationToken ) ;
204
173
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 )
207
176
{
208
177
try
209
178
{
210
- await startTask ;
211
179
await JsonSerializer . SerializeAsync ( response . BodyWriter , value , jsonTypeInfo , cancellationToken ) ;
212
180
}
213
- catch ( OperationCanceledException ) when ( ignoreOCE ) { }
181
+ catch ( OperationCanceledException ) { }
214
182
}
215
183
}
216
184
217
185
[ RequiresUnreferencedCode ( RequiresUnreferencedCodeMessage ) ]
218
186
[ RequiresDynamicCode ( RequiresDynamicCodeMessage ) ]
219
187
private static async Task WriteAsJsonAsyncSlow < TValue > (
220
- Task startTask ,
221
188
PipeWriter body ,
222
189
TValue value ,
223
190
JsonSerializerOptions ? options ,
224
- bool ignoreOCE ,
225
191
CancellationToken cancellationToken )
226
192
{
227
193
try
228
194
{
229
- await startTask ;
230
195
await JsonSerializer . SerializeAsync ( body , value , options , cancellationToken ) ;
231
196
}
232
- catch ( OperationCanceledException ) when ( ignoreOCE ) { }
197
+ catch ( OperationCanceledException ) { }
233
198
}
234
199
235
200
/// <summary>
@@ -304,42 +269,30 @@ public static Task WriteAsJsonAsync(
304
269
305
270
response . ContentType = contentType ?? ContentTypeConstants . JsonContentTypeWithCharset ;
306
271
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
-
314
272
// if no user provided token, pass the RequestAborted token and ignore OperationCanceledException
315
- if ( ! startTask . IsCompleted || ! cancellationToken . CanBeCanceled )
273
+ if ( ! cancellationToken . CanBeCanceled )
316
274
{
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 ) ;
320
277
}
321
278
322
- startTask . GetAwaiter ( ) . GetResult ( ) ;
323
279
return JsonSerializer . SerializeAsync ( response . BodyWriter , value , type , options , cancellationToken ) ;
324
280
}
325
281
326
282
[ RequiresUnreferencedCode ( RequiresUnreferencedCodeMessage ) ]
327
283
[ RequiresDynamicCode ( RequiresDynamicCodeMessage ) ]
328
284
private static async Task WriteAsJsonAsyncSlow (
329
- Task startTask ,
330
285
PipeWriter body ,
331
286
object ? value ,
332
287
Type type ,
333
288
JsonSerializerOptions ? options ,
334
- bool ignoreOCE ,
335
289
CancellationToken cancellationToken )
336
290
{
337
291
try
338
292
{
339
- await startTask ;
340
293
await JsonSerializer . SerializeAsync ( body , value , type , options , cancellationToken ) ;
341
294
}
342
- catch ( OperationCanceledException ) when ( ignoreOCE ) { }
295
+ catch ( OperationCanceledException ) { }
343
296
}
344
297
345
298
/// <summary>
@@ -367,33 +320,22 @@ public static Task WriteAsJsonAsync(
367
320
368
321
response . ContentType = contentType ?? ContentTypeConstants . JsonContentTypeWithCharset ;
369
322
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
-
377
323
// if no user provided token, pass the RequestAborted token and ignore OperationCanceledException
378
- if ( ! startTask . IsCompleted || ! cancellationToken . CanBeCanceled )
324
+ if ( ! cancellationToken . CanBeCanceled )
379
325
{
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 ) ;
383
327
}
384
328
385
- startTask . GetAwaiter ( ) . GetResult ( ) ;
386
329
return JsonSerializer . SerializeAsync ( response . BodyWriter , value , type , context , cancellationToken ) ;
387
330
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 )
390
333
{
391
334
try
392
335
{
393
- await startTask ;
394
336
await JsonSerializer . SerializeAsync ( body , value , type , context , cancellationToken ) ;
395
337
}
396
- catch ( OperationCanceledException ) when ( ignoreOCE ) { }
338
+ catch ( OperationCanceledException ) { }
397
339
}
398
340
}
399
341
0 commit comments