I solved it... If you remove the Async/Await function and change DecompressedContentAsync(response.content,compressed).Result()
That works... Dave was half way right...
Domanda
I'm having trouble writing this DecompressionHandler using the ContinueWith lambda sub function. The error I'm recieving is "The 'Async' modifier can only be used on Subs, or on Functions that return Task or Task(Of T)." within the ContinueWith sub function. I don't know what the problem or how to resolve it.
Public Class DecompressionHandler
Inherits DelegatingHandler
Public Compressors As List(Of ICompressor)
Public Sub New()
Compressors = New List(Of ICompressor)()
Compressors.Add(New GZipCompressor())
Compressors.Add(New DeflateCompressor())
End Sub
Protected Overrides Async Function SendAsync(request As HttpRequestMessage, cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)
Return Await MyBase.SendAsync(request, cancellationToken).ContinueWith(Of HttpResponseMessage)(Async Function(responseToCompleteTask)
Dim response As HttpResponseMessage = responseToCompleteTask.Result
If response.Content.Headers.ContentEncoding IsNot Nothing AndAlso response.Content IsNot Nothing Then
Dim encoding = response.Content.Headers.ContentEncoding.First()
Dim compressor = Compressors.FirstOrDefault(Function(c) c.EncodingType.Equals(encoding, StringComparison.InvariantCultureIgnoreCase))
If compressor IsNot Nothing Then response.Content = Await DecompressedContentAsync(response.Content, compressor).ConfigureAwait(False)
End If
Return response
End Function)
End Function
Private Shared Async Function DecompressedContentAsync(compressContent As HttpContent, compressor As ICompressor) As Task(Of StreamContent)
Using compressContent
Dim decompressed As New MemoryStream
Await compressor.Decompress(Await compressContent.ReadAsStreamAsync, decompressed)
Dim newContent As New StreamContent(decompressed)
newContent.Headers.ContentType = compressContent.Headers.ContentType
Return newContent
End Using
End Function
End Class
Soluzione 2
I solved it... If you remove the Async/Await function and change DecompressedContentAsync(response.content,compressed).Result()
That works... Dave was half way right...
Altri suggerimenti
VB is complaining about:
Async Function(responseToCompleteTask)
since it returns HttpResponseMessage
Change it to:
Function(responseToCompleteTask)
Just use Await
instead of ContinueWith
. My VB is rusty, but this should give you an idea:
Protected Overrides Async Function SendAsync(request As HttpRequestMessage, cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)
Dim response as HttpResponseMessage = Await MyBase.SendAsync(request, cancellationToken)
If response.Content.Headers.ContentEncoding IsNot Nothing AndAlso response.Content IsNot Nothing Then
Dim encoding = response.Content.Headers.ContentEncoding.First()
Dim compressor = Compressors.FirstOrDefault(Function(c) c.EncodingType.Equals(encoding, StringComparison.InvariantCultureIgnoreCase))
If compressor IsNot Nothing Then response.Content = Await DecompressedContentAsync(response.Content, compressor).ConfigureAwait(False)
End If
Return response
End Function