Application_Error handler isn't fired when use prefix async

Finally I've found the solution:

protected void Application_Error(object sender, EventArgs e)
{
    ...

    Task task = Task.Run(async () => await DoAsyncCall());

    task.Wait();

    ...
}

The solution that you posted uses Task.Run. What I think this means is that you will have one thread waiting for the async task to run and a second thread executing the task. This may mean that you end up using two threads.

I had a similar question but answered it by using the Server.TransferRequest function to initiate a new async request. I then did all the processing in the following request. By passing all the data via the header parameter of TransferRequest, I had an async error controller using only one thread. See Asynchronous error controller in ASP.NET MVC.

Hope this helps other people with this issue.


Comments

  1. Kye

    • 2015/6/7

    So I've defined this handler with async keyword but it isn't fired on exceptions. protected async void Application_Error(object sender, EventArgs e) { await DoAsyncCall(); } The handler is fired when I remove async keyword. BTW, I've tired to add async keyword to Application_Start handler and it works fine.

  2. Aziel

    • 2021/6/29

    How to Handle Application_error in ASP.NET App’s Global.asax. ASP.NET provides developers with an easy way to handle errors using the Global.asax and the easy-to-use Application_Error event. You can easily begin handling errors with the following code: protected void Application_Error(Object sender, EventArgs e) {//coding in here}

  3. Gallo

    • 2021/7/12

    Discover how ASP.NET Core Blazor how Blazor manages unhandled exceptions and how to develop apps that detect and handle errors.

  4. Simeon

    • 2017/10/31

    By default, an ASP.NET Core app doesn't provide a status code page for HTTP status codes, such as 404 - Not Found. The app returns a status code and an empty response body. To provide status code pages, use Status Code Pages middleware. The middleware is made available by the Microsoft.AspNetCore.Diagnostics package.

  5. Blaze

    • 2016/4/4

    @SerjSagan But it sounds like you are getting IIS Blue-screen errors, as opposed to the classic Yellow-screen errors. This makes me assume you are using some form of IIS.

  6. Wood

    • 2021/1/2

    --- protected void Application_Error(Object sender, EventArgs e) // declare an exeception object and get the original exception Exception exception = Server.GetLastError().GetBaseException();

  7. Jackson

    • 2020/10/31

    What Does Error Handling Mean? Error handling refers to the response and recovery procedures from error conditions present in a software application. In other words, it is the process comprised of anticipation, detection and resolution of application errors, programming errors or communication errors. Error handling helps in maintaining the normal flow of program execution.

Comments are closed.

Recent Posts