WCF Rest Service Download File "Semaphore Exception"

45 Views Asked by At

I am facing an issue with download a file from server with WCF rest serice. On localhost everything is working fine, but when i expose the api from static ip then remote computer is not able to download the large file (500Mb).

Below is my test scenario so far:

  1. Using webHttpBinding.
  2. With transferMode = "buffered", the file is transferring but having exception of byteArraylength.
  3. With transferMode = "streamed", the file is start download at remote browser, but stop inbetween ans server side there is an exception of "Semaphore timeout period expired".

Service contract

<ServiceContract>
Public Interface IService   
    <OperationContract()>
    <WebGet(UriTemplate:="File/{Param}", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Bare)>
    Function FileDownloadRequest(Param As String) As Stream
End Interface

<ServiceBehavior(InstanceContextMode:=InstanceContextMode.Single, ConcurrencyMode:=ConcurrencyMode.Multiple, UseSynchronizationContext:=False)>
Public Class RequestHandler
    Implements IService

Private Function FileDownloadRequest(Param As String) As Stream Implements IService.FileDownloadRequest
        Dim _FileName As String = RootFolder & "\" & Param
        Try
            ReqLogs.Add("<<-- Download request recieved for " & _FileName)
            '
            'Header Set For CORS
            '
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*")            
            If File.Exists(_FileName) Then
                WebOperationContext.Current.OutgoingResponse.Headers("Content-Disposition") = "attachment; filename=" + "Report Data.csv"
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream"
                WebOperationContext.Current.OutgoingResponse.StatusCode = Net.HttpStatusCode.OK

                Dim _FStream As New FileStream(_FileName, FileMode.Open, FileAccess.Read)
                WebOperationContext.Current.OutgoingResponse.ContentLength = _FStream.Length
                ReqLogs.Add("<<-- Download processing..... ++ " & _FStream.Length.ToString)
                Return _FStream
            Else
                ReqLogs.Add("---- File not found to download !!!")
                Throw New Exception("File not found to download")
            End If
        Catch ex As Exception
            ReqLogs.Add("Download event error: " & ex.Message)
            WebOperationContext.Current.OutgoingResponse.StatusCode = Net.HttpStatusCode.InternalServerError
            Return Nothing
        End Try
    End Function

End Class
0

There are 0 best solutions below