locked
Slow access web api RRS feed

  • Question

  • User325876037 posted
    Good evening, I can not understand the problem of my web API. everything works correctly but before giving an answer (before entering a function of a controller) it takes about 10/15 seconds. Then the call takes about 1 second. this happens both in debug and compiled. the size does not exceed 5kb so it can not be the size of the data. moreover, the waiting time takes place before the real call. where can I stop debugging to analyze the first command? How can I solve?
    Monday, December 10, 2018 6:30 PM

All replies

  • User283571144 posted

    Hi paciocco7,

    According to your description, I couldn't understand your issue clearly.

    Do you mean you find the VS start debugging and compiled project takes too long time?

    As far as I know, when you click F5 start the project, it will add the process monitor, analiysis tool or something else. So it will take a lot of time.

    Best Regards,

    Brando

    Tuesday, December 11, 2018 6:35 AM
  • User325876037 posted

    To better explain my problem, I uploaded a video. In practice, a desktop program makes a call "CallApi" and the WebApi should immediately enter the "GetRequest" instead there are 5/10 seconds of time and I do not understand why. The "CallApi" function is performed correctly, so it is not possible for the call in the desktop program to process slowly.

    Video : https://www.youtube.com/watch?v=YPrwherD-6s&feature=youtu.be

    Tuesday, December 11, 2018 6:33 PM
  • User475983607 posted

    The first call always takes longer as the application has to build.  I assume the second request is much faster.  The slowness repeats every time a new debugging session is started.

    I run a Web API without debugging when testing or writing client code.  I use unit tests to when writing the Web API.

    Tuesday, December 11, 2018 6:48 PM
  • User325876037 posted

    unfortunately it is not like that. also compiled and running IIS does not change. it always takes this time before processing the request. I do not understand the reason.

    Tuesday, December 11, 2018 7:13 PM
  • User475983607 posted

    unfortunately it is not like that. also compiled and running IIS does not change. it always takes this time before processing the request. I do not understand the reason.

    If there is always a delay then there must be something on your system like a firewall or virus checker.  Try using a network trace tool like wireshark to see what's going on.

    Tuesday, December 11, 2018 7:23 PM
  • User325876037 posted

    Code CallAPI function :

    System.Net.HttpWebRequest.DefaultWebProxy = Nothing
    ServicePointManager.DefaultConnectionLimit = 10
    getAuthorization()
    Dim origResponse As HttpWebResponse
    Dim origRequest As HttpWebRequest
    origRequest = CType(HttpWebRequest.Create(link), HttpWebRequest)
    origRequest.Headers.Add("Authorization", "Bearer " & TempToken.AccessToken)
    origRequest.AllowAutoRedirect = False
    origRequest.Method = "GET"
    origRequest.Timeout = 1000 * 60 * 10 '5 min
    origRequest.Proxy = Nothing
    Dim r As New ApiCallStatus
    Try
    origResponse = CType(origRequest.GetResponse(), HttpWebResponse)   ''' PROBLEMMMMMM
    Dim Stream As Stream = origResponse.GetResponseStream()
    Dim sr As New StreamReader(Stream, Encoding.GetEncoding("utf-8"))
    Dim str As String = sr.ReadToEnd()
    Dim jo = Json.Linq.JObject.Parse(str)
    Dim joStatus As String = "200"
    Dim joDescription As String = "Eleaborazione avvenuta correttamente"
    If jo("StatusCode") IsNot Nothing Then
    joStatus = jo("StatusCode").ToString
    joDescription = jo("ReasonPhrase").ToString
    End If
    r.Result = str
    r.Message = joDescription
    r.Link = link
    r.StatusCode = joStatus
    r.StatusDescription = joDescription

    Catch exRequest As System.Net.WebException
    r.Result = "WebException"
    r.Link = link
    Try
    Dim HWR As HttpWebResponse
    HWR = (CType(exRequest.Response, HttpWebResponse))
    r.StatusCode = HWR.StatusCode
    r.StatusDescription = HWR.StatusDescription
    Catch exConversion As Exception
    r.StatusCode = "0"
    r.StatusDescription = exConversion.Message
    End Try
    r.StatusError = True
    End Try

    Thursday, December 13, 2018 7:01 PM
  • User325876037 posted

    can someone help me to make this function asynchronous?

    Thursday, December 13, 2018 7:01 PM
  • User283571144 posted

    Hi paciocco7,

    According to your description, I suggest you could try to use GetResponseAsync method to make the function become asynchronous.

    More details, you could refer to below codes:

     
        Public Async Function SendRequestAsync(ByVal link As String) As Threading.Tasks.Task
            System.Net.HttpWebRequest.DefaultWebProxy = Nothing
            'ServicePointManager.DefaultConnectionLimit = 10
            'getAuthorization()
            Dim origResponse As HttpWebResponse
            Dim origRequest As HttpWebRequest
            origRequest = CType(HttpWebRequest.Create(link), HttpWebRequest)
            'origRequest.Headers.Add("Authorization", "Bearer " & TempToken.AccessToken)
            origRequest.AllowAutoRedirect = False
            origRequest.Method = "GET"
            origRequest.Timeout = 1000 * 60 * 10 '5 min
            origRequest.Proxy = Nothing
            Dim r As New ApiCallStatus
            Try
    
                origResponse = CType(Await origRequest.GetResponseAsync(), HttpWebResponse)   ''' PROBLEMMMMMM
                Dim Stream As Stream = origResponse.GetResponseStream()
                Dim sr As New StreamReader(Stream, Encoding.GetEncoding("utf-8"))
                Dim str As String = sr.ReadToEnd()
                Dim jo = Json.Linq.JObject.Parse(str)
                Dim joStatus As String = "200"
                Dim joDescription As String = "Eleaborazione avvenuta correttamente"
                If jo("StatusCode") IsNot Nothing Then
                    joStatus = jo("StatusCode").ToString
                    joDescription = jo("ReasonPhrase").ToString
                End If
                r.Result = str
                r.Message = joDescription
                r.Link = link
                r.StatusCode = joStatus
                r.StatusDescription = joDescription
    
            Catch exRequest As System.Net.WebException
                r.Result = "WebException"
                r.Link = link
                Try
                    Dim HWR As HttpWebResponse
                    HWR = (CType(exRequest.Response, HttpWebResponse))
                    r.StatusCode = HWR.StatusCode
                    r.StatusDescription = HWR.StatusDescription
                Catch exConversion As Exception
                    r.StatusCode = "0"
                    r.StatusDescription = exConversion.Message
                End Try
                r.StatusError = True
            End Try
        End Function
    
    
        Public Class ApiCallStatus
            Public Result As String
            Public Message As String
            Public Link As String
            Public StatusCode As String
            Public StatusDescription As String
            Public StatusError As String
    
        End Class
     

    Best Regards,

    Brando

    Tuesday, December 18, 2018 3:18 AM