locked
How do I solve HTTP 500 (the request timed out) error on a hosted web app. RRS feed

  • Question

  • User-1146609079 posted

    Hi,I have a web app that i have deployed on azure and after a short while I get HTTP 500 (The request timed out). sometimes the homepage comes up sometimes it doesn't,Is there anything I can do to fix this? when its running locally it has no problem.

    I have tried re-deploying the app with different publish settings, and checking the server settings.

    Thursday, September 20, 2018 9:30 PM

All replies

  • User1120430333 posted

    The HTTP 500 error is a catch 22 error. The WebAPI blew up with a .NET exception being thrown, you don't have any global exception handling to catch the exception and log  it. So the Web server swallowed the exception and threw the 500. 

    https://stackify.com/csharp-catch-all-exceptions/

    I implemented it is a WebAPI using VB.NET.

    Imports System.Net
    Imports System.Net.Http
    Imports System.Web.Http.Filters
    Imports log4net
    
    Public Class CustomExceptionFilter
        Inherits ExceptionFilterAttribute
    
        private ReadOnly _logger As ILog
    
        public sub New()
            _logger = LogManager.GetLogger(GetType(CustomExceptionFilter))
        End sub
    
        Public Overrides Sub OnException(actionExecutedContext As HttpActionExecutedContext)
            MyBase.OnException(actionExecutedContext)
    
            Dim exceptionMessage as String = String.Empty
    
            If IsNothing(actionExecutedContext.Exception.InnerException ) Then
                exceptionMessage = actionExecutedContext.Exception.Message _
                                   & " " & actionExecutedContext.Exception.StackTrace
            Else 
                exceptionMessage =  actionExecutedContext.Exception.Message _
                                    & " " & actionExecutedContext.Exception.InnerException.Message _
                                    & " " & actionExecutedContext.Exception.StackTrace
            End If
    
            _logger.Error(exceptionMessage)
    
            dim response =  New HttpResponseMessage(HttpStatusCode.InternalServerError)With {.Content = New StringContent(“An unhandled exception was thrown by service.”),
                    .ReasonPhrase = "Internal Server Error.Please Contact your Administrator."}
    
            actionExecutedContext.Response = response
    
        End Sub
    End Class
    
    Imports System.Web.Http
    Imports DAL
    Imports Entities
    
    Namespace Controllers
    
        <CustomExceptionFilter>
        Public Class ProjectController
            Inherits ApiController
    
            Private ReadOnly _daoproject As IDaoProject
    
            public sub New (daoproject As IDaoProject)
                _daoproject = daoproject
            End sub
    
            <HttpGet>
            <ActionName("GetProjectById")>
            public Function GetProjectById(ByVal id As Int32) As DtoProject
                return _daoproject.GetProjectById(id)
            End Function
    
    
            <HttpGet>
            <ActionName("GetProjectsByUserId")>
            public Function GetProjectsByUserId(ByVal userid As String) As List(Of DtoProject)
                return _daoproject.GetProjectsByUserId(userid)
            End Function
    
            <HttpPost>
            <ActionName("CreateProject")>
            public sub CreateProject(ByVal dto As DtoProject)
                Call _daoproject.CreateProject(dto)
            End sub
            
            <HttpPost>
            <ActionName("UpdateProject")>
            public sub UpdateProject(ByVal dto As DtoProject)
                Call _daoproject.UpdateProject(dto)
            End sub
    
            <HttpPost>
            <ActionName("DeleteProject")>
            public sub  DeleteProject(ByVal dto As DtoId)
                Call _daoproject.DeleteProject(dto.Id)
            End sub
            
        End Class
    End Namespace

    Thursday, September 20, 2018 11:26 PM
  • User61956409 posted

    Hi lindokuhle ndaba,

    I suspect that 230 seconds timeout limitation for request causes the issue. As David Ebbo mentioned in this thread:

    There is a 230 second (i.e. a little less than 4 mins) timeout for requests that are not sending any data back. 
    After that, the client gets the 500 you saw, even though in reality the request is allowed to continue server side.

    To troubleshoot the issue, please check if any logic processing take long time to complete and return response in your code.

    With Regards,

    Fei Han

    Friday, September 21, 2018 2:11 AM