我有一个同步端点,该端点允许您下载文件,并且希望它变得异步。查看以下文档(https://docs.microsoft.com/en-us/dotnet/framework/wcf/how-to-implement-an-asynchronous-service-operation),我以这种方式修改了代码,但是从测试中我发现,文件的恢复(由“ New FileStream”确定)仍然是同步的。
<OperationContractAttribute(AsyncPattern:=True)>
<WebGet(UriTemplate:="RecuperaPacchettoFinitoAsync?l={sLoginName}&f={sFilename}&g={sGuid}&tp={sTipoProdotto}&v={sVersione}&bi={sBuildIniziale}&bf={sBuildFinale}&bd={bDocumentazione}&t={token}")>
Function BeginDownloadFileAsync(sLoginName As String, sFilename As String, sGuid As String, sTipoProdotto As String, sversione As String, sBuildIniziale As String, sBuildFinale As String, bDocumentazione As Boolean, token As String, callback As AsyncCallback, asyncState As Object) As IAsyncResult
Function EndDownloadFileAsync(result As IAsyncResult) As Stream
...
Private Function BeginDownloadFileAsync(sloginName As String, sFilename As String, sGuid As String, sTipoProdotto As String, sVersione As String, sBuildIniziale As String, sBuildFinale As String, bDocumentazione As Boolean, sToken As String, callback As AsyncCallback, asyncState As Object) As IAsyncResult Implements ILiveUpdateUIWS.BeginDownloadFileAsync
If (LU_Helper.ValidaToken(sToken, DB_CONNECTION_STRING) = True) Then
Try
Dim sFilenameInCache As String = String.Format("{0}{1}_{2}_{3}.zip", sTipoProdotto, sVersione, sBuildIniziale.PadLeft(10, "0"c), sBuildFinale.PadLeft(10, "0"c))
If sTipoProdotto = "SampleProduct" Then
sFilenameInCache = sFilename
End If
If (My.Computer.FileSystem.FileExists(String.Format("{0}\{1}", DESTINATION_PATH_PACKAGES_COMPLETED, sFilenameInCache)) AndAlso bDocumentazione = False) Then
Dim downloadFilePath As String = String.Format("{0}\{1}", DESTINATION_PATH_PACKAGES_COMPLETED, sFilenameInCache)
WebOperationContext.Current.OutgoingResponse.ContentType = "application/zip"
Dim fstream = New FileStream(downloadFilePath, FileMode.Open)
Return New CompletedAsyncResult(Of Stream)(fstream)
Else
Dim downloadFilePath As String = String.Format("{0}\{1}\{2}\{3}", DESTINATION_PATH_ZIP_FILE, sloginName, sGuid, sFilename)
WebOperationContext.Current.OutgoingResponse.ContentType = "application/zip"
Dim fstream = New FileStream(downloadFilePath, FileMode.Open)
Return New CompletedAsyncResult(Of Stream)(fstream)
End If
Catch ex As Exception
TraceLog.WriteToErrorLog(ex.Message, Environment.StackTrace, "Error")
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.InternalServerError
Return Nothing
End Try
Else
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden
Return Nothing
End If
End Function
Private Function EndDownloadFileAsync(r As IAsyncResult) As Stream Implements ILiveUpdateUIWS.EndDownloadFileAsync
Dim result As CompletedAsyncResult(Of Stream) = TryCast(r, CompletedAsyncResult(Of Stream))
Return result.Data
End Function
...
Class CompletedAsyncResult(Of T)
Implements IAsyncResult
Private datalocal As T
Public Sub New(ByVal data As T)
datalocal = data
End Sub
Public ReadOnly Property Data As T
Get
Return datalocal
End Get
End Property
Public ReadOnly Property AsyncState As Object Implements IAsyncResult.AsyncState
Get
Return CObj(datalocal)
End Get
End Property
Public ReadOnly Property AsyncWaitHandle As WaitHandle Implements IAsyncResult.AsyncWaitHandle
Get
Throw New Exception("The method or operation is not implemented.")
End Get
End Property
Public ReadOnly Property CompletedSynchronously As Boolean Implements IAsyncResult.CompletedSynchronously
Get
Return True
End Get
End Property
Public ReadOnly Property IsCompleted As Boolean Implements IAsyncResult.IsCompleted
Get
Return True
End Get
End Property
End Class