Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

[BindProperty(BinderType = typeof(SimpleTypeModelBinder)] no longer works #7354

Closed
dougbu opened this issue Feb 12, 2018 · 6 comments
Closed

Comments

@dougbu
Copy link
Member

dougbu commented Feb 12, 2018

Though we've added new constructors to most model binders in a backward-compatible fashion, the change actually breaks a scenario. For example, [BindProperty(BinderType = typeof(SimpleTypeModelBinder)] and [ModelBinder(BinderType = typeof(SimpleTypeModelBinder)] now result in

An unhandled exception occurred while processing the request.
InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder'. There should only be one applicable constructor.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, out ConstructorInfo matchingConstructor, out Nullable<int>[] parameterMap)

Stack Query Cookies Headers
InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder'. There should only be one applicable constructor.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, out ConstructorInfo matchingConstructor, out Nullable<int>[] parameterMap)
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactory(Type instanceType, Type[] argumentTypes)
Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BinderTypeModelBinder..ctor(Type binderType)
Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BinderTypeModelBinderProvider.GetBinder(ModelBinderProviderContext context)
...
@mkArtakMSFT
Copy link
Member

This is an effect of ActivatorUtilities/multiple constructors issue. This specific use-case is not common, so this is a low priority issue.

@mkArtakMSFT mkArtakMSFT added this to the 2.2.0 milestone Feb 12, 2018
@mkArtakMSFT
Copy link
Member

The real fix will be handled by a fix we consider for #7330

@pranavkm
Copy link
Contributor

Going back all the way to 1.0 - SimpleTypeModelBinder expects a type parameter to be passed in that BinderTypeModelBinder - https://github.com/aspnet/Mvc/blob/release/1.0/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Binders/BinderTypeModelBinder.cs#L40 never passed in.

The reason you're seeing the error at all is because the two constructors are treated as equivalent since neither can be resolved. Should this be closed as invalid?

@dougbu
Copy link
Member Author

dougbu commented May 10, 2018

@pranavkm I believe I was updating a project I'd used in previous validation rounds when I hit this. But, I don't think I have that test system anymore.

In any case, I'll retry the scenario with typeof(BodyTypeModelBinder) and see if I hid an ActivatorUtilities issue with that type too.

@pranavkm
Copy link
Contributor

A bit of a tangent, but the current solution for disambiguating ctors in ActivatorUtilities is to annotate the preferred one with an attribute. If we run in to this sort of scenario frequently enough, we could consider adding a negative preference to ctors with Obsolete attribute. That said, so far most of the ctors that we've added generally add additional parameters that come from DI which lends itself to the disambiguation algorithm.

@mkArtakMSFT
Copy link
Member

Closing this as it seems the underlying issue has been fixed already: dotnet/aspnetcore#2871

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants