Skip to content
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

Adding additional comment about FromServices optionality #25111

Merged
merged 2 commits into from
Feb 23, 2022

Conversation

brunolins16
Copy link
Member

Fix #25106.

Comment on lines 628 to 629
> [!TIP]
> The binding will fail when obtaining an instance of a type, **not registered**, from the dependency injection container. If the service is expected to be **optional** then the parameter must be marked as nullable, including `?`, or set a default value. In this case, the method is responsible for the required check before the usage.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> [!TIP]
> The binding will fail when obtaining an instance of a type, **not registered**, from the dependency injection container. If the service is expected to be **optional** then the parameter must be marked as nullable, including `?`, or set a default value. In this case, the method is responsible for the required check before the usage.
> [!TIP]
> Binding fails when attempting to bind to a type that isn't registered with the dependency injection container or isn't a .NET type. If the service is **optional**, the parameter must be one of the following:
>* Marked as [nullable](/dotnet/csharp/language-reference/builtin-types/nullable-value-types).
>* Have a default value.
>
> When using optional parameters, the app must check if the parameter is available.

This might be closer. @serpent5 can you help out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made an alternative suggestion below. I don't think this needs to be a tip, and I don't think we should link to the nullable docs as they're specific to value-types.

Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com>
@Rick-Anderson Rick-Anderson merged commit f7baf7c into dotnet:main Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FromServices could returns null/default when optional
3 participants