-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Add support for implicit inference of FromServices
for types that appear in DI
#39667
Comments
One aspect of this implementation, in comparison with Minimal, that we could consider as an additional enhancement is if the parameter is
|
Thanks for contacting us. We're moving this issue to the |
I added a new issue for this #39757 |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
We just approved |
I agree with you, we definitely should use the same. I completely forgot about the API approved in SignalR and suggested the name with the wording |
In my draft implementation the following API change might be also needed: namespace Microsoft.AspNetCore.Mvc.ApplicationModels;
public class InferParameterBindingInfoConvention : IActionModelConvention
{
+ public InferParameterBindingInfoConvention(
+ IModelMetadataProvider modelMetadataProvider,
+ IServiceProviderIsService? serviceProviderIsService){}
} |
API review: Approved including the new constructor (note that it's has a non-null 2nd parameter) namespace Microsoft.AspNetCore.Mvc.ApplicationModels;
public class InferParameterBindingInfoConvention : IActionModelConvention
{
+ public InferParameterBindingInfoConvention(
+ IModelMetadataProvider modelMetadataProvider,
+ IServiceProviderIsService serviceProviderIsService){}
}
|
Announcement Discussion: #40071 |
Current design
With the Minimal APIs, what the
RequestDelegateFactory
does is fall back to try check if the Parameter is a Service registered in the DI when:and
HttpContext
,HttpRequest
,HttpResponse
,ClaimsPrincipal
,CancellationToken
,IFormFileCollection
andIFormFile
)and
BindAsync
methodand
string
and
TryParse
methodCurrently MVC do not have support for items 3 and 4.
The way MVC implements the
ModelBinding
logic is throughModelBinders
that will be assigned, by Provider (Eg.ComplexObjectModelBinderProvider
) based on the Parameter metadata.Also, the binding
FromServices
is already implemented (ServicesModelBinderProvider
/ServicesModelBinder
) that will be used when theBindingSource
is set to Services.For API Controllers, the BindingSource will be inferred, when the metadata is not set yet, based on with the following logic:
BindingSource = Body
BindingSource = Path
BindingSource = Query
In addition to that, Item 2 is similar in MVC since the
Special
orFormFile
binding source is set byBindingSourceMetadataProvider
, except forHttpContext
,HttpRequest
andHttpResponse
that are available in theControllerBase
classEg.:
Based on the previous logic, nothing will be inferred as Services and the only way to the
BindingSource
to be set toServices
is using the[FromServices]
attribute.Proposed Change
Update the Infer mechanism to do the following:
aspnetcore/src/Mvc/Mvc.Core/src/ApplicationModels/InferParameterBindingInfoConvention.cs
Line 94 in 8bd8f58
a. Is Registered in the DI =>
BindingSource = Services
b. No registered =>
BindingSource = Body
a. Is part of any route =>
BindingSource = Path
BindingSource = Query
My proposal is to use the
IServiceProviderIsService
service, same used by theRequestDelegateFactory
, injected in the constructor.public InferParameterBindingInfoConvention( IModelMetadataProvider modelMetadataProvider, + IServiceProviderIsService? serviceProviderIsService = null)
And update the
InferBindingSourceForParameter
method to verify if the parameter type is registered in the DI.That will cover the idea of implicit inference of
FromService
since theServiceModelBinder
will be activated when we set theBindingSource
toServices
.Also, I prefer this to be the new default behavior, so my suggestion is to include allowing users to opt-out:
namespace Microsoft.AspNetCore.Mvc; public class ApiBehaviorOptions { + public bool SuppressInferBindingFromServicesForParameters { get; set; } }
Usage Examples
The text was updated successfully, but these errors were encountered: