的Application_Error处理器没有发射时使用preFIX异步处理器、Application_Error、preFIX

2023-09-04 02:48:26 作者:讨心酸

我要做出的Application_Error处理程序的异步调用。所以我定义这个处理器与异步关键字,但它不是射击例外。

I have to make an asynchronous call in Application_Error handler. 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.

顺便说一句,我已经累到异步关键字添加到的Application_Start处理程序,它工作正常。

BTW, I've tired to add async keyword to Application_Start handler and it works fine.

我怎样才能让的Application_Error异步?或者,我该怎么做,而不异步关键字异步调用?

How can I make Application_Error async? Or how can I make asynchronous calls without async keyword?

推荐答案

这是您发布的解决方案使用Task.Run。我认为这意味着你将有一个线程等待异步任务运行和第二个线程执行任务。这可能意味着你最终使用两个线程。

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.

我也有类似的问题,但通过使用Server.TransferRequest功能,启动新的异步请求,回答了这个问题。然后我做了以下要求所有的处理。通过经由TransferRequest的报头参数传递的所有数据,我只使用一个线程异步错误控制器。请参阅Asynchronous在ASP.NET MVC 的错误控制器。

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.