From f7c2b1868b69f437f8ec28b9136bc3d28e3618a3 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 22 Feb 2022 16:25:02 -0800 Subject: [PATCH 1/2] Adding additional comment about FromServices optionality --- aspnetcore/mvc/models/model-binding.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aspnetcore/mvc/models/model-binding.md b/aspnetcore/mvc/models/model-binding.md index c5f866e1b22e..26f37f0b2a6a 100644 --- a/aspnetcore/mvc/models/model-binding.md +++ b/aspnetcore/mvc/models/model-binding.md @@ -625,6 +625,9 @@ For more information, see [TryUpdateModelAsync](xref:data/ef-rp/crud#TryUpdateMo This attribute's name follows the pattern of model binding attributes that specify a data source. But it's not about binding data from a value provider. It gets an instance of a type from the [dependency injection](xref:fundamentals/dependency-injection) container. Its purpose is to provide an alternative to constructor injection for when you need a service only if a particular method is called. +> [!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. + ## Additional resources * [View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/mvc/models/model-binding/samples) ([how to download](xref:index#how-to-download-a-sample)) From 6a75b0dedfacb006f73fb5fcf9a08d73554249f0 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 23 Feb 2022 09:14:41 -0800 Subject: [PATCH 2/2] Update aspnetcore/mvc/models/model-binding.md Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> --- aspnetcore/mvc/models/model-binding.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/aspnetcore/mvc/models/model-binding.md b/aspnetcore/mvc/models/model-binding.md index 26f37f0b2a6a..5c0d47bd8e48 100644 --- a/aspnetcore/mvc/models/model-binding.md +++ b/aspnetcore/mvc/models/model-binding.md @@ -625,8 +625,12 @@ For more information, see [TryUpdateModelAsync](xref:data/ef-rp/crud#TryUpdateMo This attribute's name follows the pattern of model binding attributes that specify a data source. But it's not about binding data from a value provider. It gets an instance of a type from the [dependency injection](xref:fundamentals/dependency-injection) container. Its purpose is to provide an alternative to constructor injection for when you need a service only if a particular method is called. -> [!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. +If an instance of the type isn't registered in the dependency injection container, the app throws an exception when attempting to bind the parameter. To make the parameter optional, use one of the following approaches: + +* Make the parameter nullable. +* Set a default value for the parameter. + +For nullable parameters, ensure that the parameter isn't `null` before accessing it. ## Additional resources