问题
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