Aller au contenu

Async lambda (ARCH107)

Ce contenu n’est pas encore disponible dans votre langue.

Key dotnet.async.async-lambda · Severity warning · Category async

Async lambdas that bind to an async void signature are dangerous; exceptions inside them cannot be caught. Add proper error handling, or prefer Task-returning delegates.

timer.Elapsed += async (s, e) =>
{
await _repository.SaveAsync();
};
timer.Elapsed += async (s, e) =>
{
try
{
await _repository.SaveAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Save failed");
}
};

Wrap the async lambda body in try/catch, or switch to a Func<Task> delegate. If needed, suppress the rule per project/workspace via except/ignore.