Azure WebJobs, catch error to prevent host failure

吃可爱长大的小学妹 提交于 2020-03-05 04:08:10

问题


Assume a simple HostBuilder configuration as described here.

Just like that:

static async Task Main()
{
    var builder = new HostBuilder();
    builder.ConfigureWebJobs(b => b
           .AddAzureStorageCoreServices()
           .AddAzureStorage()
           .AddTimers())
    var host = builder.Build();
    using (host)
    {
        await host.RunAsync();
    }
}

There seem to be some situations where Exceptions can happen, not inside the QueueTrigger or TimerTrigger functions directly (these are catched gracefully), but on a global scope. Some of them I already noticed were HTTP 409 Conflict responses, TimeoutException raised, etc (they seem to happen right before entering the actual Function, e.g. when accessing the Queue for Messages read). And the whole host app is shut down. The Webjob needs to restart and I don't want that. The point is, I don't want to tackle these specific issues here.

How can I catch the exception and prevent the host from being destroyed? It seems that I can't even put a try/catch around await host.RunAsync(), the catch was not triggered. I added Logging just before the host run and inside the catch; After some time I had multiple "Starting..." logs but no "Error occured" log.

来源:https://stackoverflow.com/questions/60304305/azure-webjobs-catch-error-to-prevent-host-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!