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

dotnet core 3.0 - Unable to create an instance from Microsoft.AspNetCore.Mvc.ModelBinding.BindingInfo #1141

Closed
TroyHouston opened this issue Sep 26, 2019 · 6 comments

Comments

@TroyHouston
Copy link

I am currently in the process of upgrading a simple project to dotnet core 3. This project has a number of unit tests, the majority against the controller/api layer using AutoFixture & xunit2.

After the upgrade everything is working EXCEPT the autofixture unit tests that attempt to create a controller. They fail with the following exception:

AutoFixture.ObjectCreationExceptionWithPath : AutoFixture was unable to create an instance from Microsoft.AspNetCore.Mvc.ModelBinding.BindingInfo because creation unexpectedly failed with exception. Please refer to the inner exception to investigate the root cause of the failure.

Request path:
****.Inventory.Api.Controllers.ProductCategoriesController controller
****.Inventory.Api.Controllers.ProductCategoriesController
Microsoft.AspNetCore.Mvc.ControllerContext ControllerContext
Microsoft.AspNetCore.Mvc.ControllerContext
Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor ActionDescriptor
Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor
System.Collections.Generic.IList1[Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor] Parameters System.Collections.Generic.IList1[Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor]
System.Collections.Generic.List1[Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor] System.Collections.Generic.IEnumerable1[Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor] collection
System.Collections.Generic.IEnumerable`1[Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor]
Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor
Microsoft.AspNetCore.Mvc.ModelBinding.BindingInfo BindingInfo
Microsoft.AspNetCore.Mvc.ModelBinding.BindingInfo

Inner exception messages:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
System.ArgumentException: The type 'System.Object' must implement 'Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder' to be used as a model binder. (Parameter 'value')

@Fjsmoreira
Copy link

@TroyHouston I've ran into this issue as well. I've fixed it by using the [Greedy] Attribute on my controller.

I'm using Nunit :)

@TroyHouston
Copy link
Author

@Fjsmoreira Thanks! That also works for me :)

@jonjon1123
Copy link

I know this issue is closed, but I need some help understanding where the [Greedy] Attribute comes from. @Fjsmoreira or @TroyHouston, what package/library do I need to use to get the [Greedy] Attribute?

@jonjon1123
Copy link

I know this issue is closed, but I need some help understanding where the [Greedy] Attribute comes from. @Fjsmoreira or @TroyHouston, what package/library do I need to use to get the [Greedy] Attribute?

Never mind. Figured it out! Looks like it is part of the AutoFixture.Xunit package.

@billdinger
Copy link

I know this is closed but for other folks who run into this who arent using Xunit the fast (lazy) workaround is to do

Fixture.Customize<BindingInfo>(c => c.OmitAutoProperties()); -- I'm sure there is a way to also fix bindingInfo if you need it but if you dont in your tests...

@DaanAcohen
Copy link

Solved the problem with OmitAutoProperties:
var controller = fixture.Build<SomeController>().OmitAutoProperties().Create();

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

No branches or pull requests

5 participants