-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Host: log exception if a service factory fails #112534
base: main
Are you sure you want to change the base?
Conversation
Tagging subscribers to this area: @dotnet/area-extensions-hosting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
// service factory or validation failed, abort startup. | ||
exceptions.Add(ex); | ||
LogAndRethrow(); | ||
return; // unreachable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend either removing this return
, or making it consistent with the rest of the code here.
There's also [DoesNotReturn]
that can be applies to LogAndRethrow()
but that would be just for readability in this case I believe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return;
is necessary here to avoid a null reference warning for _hostedServices
below. [DoesNotReturn]
is not possible because LogAndRethrow()
does not actually throw if exceptions
is empty.
cc @halter73 for any feedback. Thanks |
Currently, the host doesn't log any exception if a
IHostedService
orIStartupValidator
service factory fails. If you useRunAsync
you also have no way to log it yourself since the host has already been disposed after it returns (see also #72225).This catches any exceptions thrown by the
GetService
calls and logs and re-throws the exception, similiarly on how it is already done for exceptions inStartAsync
etc. .