@@ -67,18 +67,20 @@ public Task<string> ReadAsStringAsync()
67
67
{
68
68
CheckDisposed ( ) ;
69
69
70
- TaskCompletionSource < string > tcs = new TaskCompletionSource < string > ( ) ;
70
+ var tcs = new TaskCompletionSource < string > ( this ) ;
71
71
72
- LoadIntoBufferAsync ( ) . ContinueWithStandard ( task =>
72
+ LoadIntoBufferAsync ( ) . ContinueWithStandard ( tcs , ( task , state ) =>
73
73
{
74
- if ( HttpUtilities . HandleFaultsAndCancelation ( task , tcs ) )
74
+ var innerTcs = ( TaskCompletionSource < string > ) state ;
75
+ var innerThis = ( HttpContent ) innerTcs . Task . AsyncState ;
76
+ if ( HttpUtilities . HandleFaultsAndCancelation ( task , innerTcs ) )
75
77
{
76
78
return ;
77
79
}
78
80
79
- if ( _bufferedContent . Length == 0 )
81
+ if ( innerThis . _bufferedContent . Length == 0 )
80
82
{
81
- tcs . TrySetResult ( string . Empty ) ;
83
+ innerTcs . TrySetResult ( string . Empty ) ;
82
84
return ;
83
85
}
84
86
@@ -90,21 +92,21 @@ public Task<string> ReadAsStringAsync()
90
92
Encoding encoding = null ;
91
93
int bomLength = - 1 ;
92
94
93
- byte [ ] data = this . GetDataBuffer ( _bufferedContent ) ;
95
+ byte [ ] data = innerThis . GetDataBuffer ( innerThis . _bufferedContent ) ;
94
96
95
- int dataLength = ( int ) _bufferedContent . Length ; // Data is the raw buffer, it may not be full.
97
+ int dataLength = ( int ) innerThis . _bufferedContent . Length ; // Data is the raw buffer, it may not be full.
96
98
97
99
// If we do have encoding information in the 'Content-Type' header, use that information to convert
98
100
// the content to a string.
99
- if ( ( Headers . ContentType != null ) && ( Headers . ContentType . CharSet != null ) )
101
+ if ( ( innerThis . Headers . ContentType != null ) && ( innerThis . Headers . ContentType . CharSet != null ) )
100
102
{
101
103
try
102
104
{
103
- encoding = Encoding . GetEncoding ( Headers . ContentType . CharSet ) ;
105
+ encoding = Encoding . GetEncoding ( innerThis . Headers . ContentType . CharSet ) ;
104
106
}
105
107
catch ( ArgumentException e )
106
108
{
107
- tcs . TrySetException ( new InvalidOperationException ( SR . net_http_content_invalid_charset , e ) ) ;
109
+ innerTcs . TrySetException ( new InvalidOperationException ( SR . net_http_content_invalid_charset , e ) ) ;
108
110
return ;
109
111
}
110
112
}
@@ -143,11 +145,11 @@ public Task<string> ReadAsStringAsync()
143
145
{
144
146
// Drop the BOM when decoding the data.
145
147
string result = encoding . GetString ( data , bomLength , dataLength - bomLength ) ;
146
- tcs . TrySetResult ( result ) ;
148
+ innerTcs . TrySetResult ( result ) ;
147
149
}
148
150
catch ( Exception ex )
149
151
{
150
- tcs . TrySetException ( ex ) ;
152
+ innerTcs . TrySetException ( ex ) ;
151
153
}
152
154
} ) ;
153
155
@@ -158,13 +160,15 @@ public Task<byte[]> ReadAsByteArrayAsync()
158
160
{
159
161
CheckDisposed ( ) ;
160
162
161
- TaskCompletionSource < byte [ ] > tcs = new TaskCompletionSource < byte [ ] > ( ) ;
163
+ var tcs = new TaskCompletionSource < byte [ ] > ( this ) ;
162
164
163
- LoadIntoBufferAsync ( ) . ContinueWithStandard ( task =>
165
+ LoadIntoBufferAsync ( ) . ContinueWithStandard ( tcs , ( task , state ) =>
164
166
{
165
- if ( ! HttpUtilities . HandleFaultsAndCancelation ( task , tcs ) )
167
+ var innerTcs = ( TaskCompletionSource < byte [ ] > ) state ;
168
+ var innerThis = ( HttpContent ) innerTcs . Task . AsyncState ;
169
+ if ( ! HttpUtilities . HandleFaultsAndCancelation ( task , innerTcs ) )
166
170
{
167
- tcs . TrySetResult ( _bufferedContent . ToArray ( ) ) ;
171
+ innerTcs . TrySetResult ( innerThis . _bufferedContent . ToArray ( ) ) ;
168
172
}
169
173
} ) ;
170
174
@@ -175,7 +179,7 @@ public Task<Stream> ReadAsStreamAsync()
175
179
{
176
180
CheckDisposed ( ) ;
177
181
178
- TaskCompletionSource < Stream > tcs = new TaskCompletionSource < Stream > ( ) ;
182
+ TaskCompletionSource < Stream > tcs = new TaskCompletionSource < Stream > ( this ) ;
179
183
180
184
if ( _contentReadStream == null && IsBuffered )
181
185
{
@@ -194,12 +198,14 @@ public Task<Stream> ReadAsStreamAsync()
194
198
return tcs . Task ;
195
199
}
196
200
197
- CreateContentReadStreamAsync ( ) . ContinueWithStandard ( task =>
201
+ CreateContentReadStreamAsync ( ) . ContinueWithStandard ( tcs , ( task , state ) =>
198
202
{
199
- if ( ! HttpUtilities . HandleFaultsAndCancelation ( task , tcs ) )
203
+ var innerTcs = ( TaskCompletionSource < Stream > ) state ;
204
+ var innerThis = ( HttpContent ) innerTcs . Task . AsyncState ;
205
+ if ( ! HttpUtilities . HandleFaultsAndCancelation ( task , innerTcs ) )
200
206
{
201
- _contentReadStream = task . Result ;
202
- tcs . TrySetResult ( _contentReadStream ) ;
207
+ innerThis . _contentReadStream = task . Result ;
208
+ innerTcs . TrySetResult ( innerThis . _contentReadStream ) ;
203
209
}
204
210
} ) ;
205
211
@@ -232,19 +238,20 @@ public Task CopyToAsync(Stream stream, TransportContext context)
232
238
}
233
239
234
240
// If the copy operation fails, wrap the exception in an HttpRequestException() if appropriate.
235
- task . ContinueWithStandard ( copyTask =>
241
+ task . ContinueWithStandard ( tcs , ( copyTask , state ) =>
236
242
{
243
+ var innerTcs = ( TaskCompletionSource < object > ) state ;
237
244
if ( copyTask . IsFaulted )
238
245
{
239
- tcs . TrySetException ( GetStreamCopyException ( copyTask . Exception . GetBaseException ( ) ) ) ;
246
+ innerTcs . TrySetException ( GetStreamCopyException ( copyTask . Exception . GetBaseException ( ) ) ) ;
240
247
}
241
248
else if ( copyTask . IsCanceled )
242
249
{
243
- tcs . TrySetCanceled ( ) ;
250
+ innerTcs . TrySetCanceled ( ) ;
244
251
}
245
252
else
246
253
{
247
- tcs . TrySetResult ( null ) ;
254
+ innerTcs . TrySetResult ( null ) ;
248
255
}
249
256
} ) ;
250
257
}
@@ -354,15 +361,17 @@ public Task LoadIntoBufferAsync(long maxBufferSize)
354
361
355
362
protected virtual Task < Stream > CreateContentReadStreamAsync ( )
356
363
{
357
- TaskCompletionSource < Stream > tcs = new TaskCompletionSource < Stream > ( ) ;
364
+ var tcs = new TaskCompletionSource < Stream > ( this ) ;
358
365
// By default just buffer the content to a memory stream. Derived classes can override this behavior
359
366
// if there is a better way to retrieve the content as stream (e.g. byte array/string use a more efficient
360
367
// way, like wrapping a read-only MemoryStream around the bytes/string)
361
- LoadIntoBufferAsync ( ) . ContinueWithStandard ( task =>
368
+ LoadIntoBufferAsync ( ) . ContinueWithStandard ( tcs , ( task , state ) =>
362
369
{
363
- if ( ! HttpUtilities . HandleFaultsAndCancelation ( task , tcs ) )
370
+ var innerTcs = ( TaskCompletionSource < Stream > ) state ;
371
+ var innerThis = ( HttpContent ) innerTcs . Task . AsyncState ;
372
+ if ( ! HttpUtilities . HandleFaultsAndCancelation ( task , innerTcs ) )
364
373
{
365
- tcs . TrySetResult ( _bufferedContent ) ;
374
+ innerTcs . TrySetResult ( innerThis . _bufferedContent ) ;
366
375
}
367
376
} ) ;
368
377
0 commit comments