Skip to content

Adding additional comment about FromServices optionality#25111

Merged
Rick-Anderson merged 2 commits into
dotnet:mainfrom
brunolins16:brunolins16/issues/25106
Feb 23, 2022
Merged

Adding additional comment about FromServices optionality#25111
Rick-Anderson merged 2 commits into
dotnet:mainfrom
brunolins16:brunolins16/issues/25106

Conversation

@brunolins16
Copy link
Copy Markdown
Member

Fix #25106.

Comment thread aspnetcore/mvc/models/model-binding.md Outdated
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
Copy Markdown
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
Copy Markdown
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.

Comment thread aspnetcore/mvc/models/model-binding.md Outdated
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