question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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’)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:6

github_iconTop GitHub Comments

34reactions
billdingercommented, Oct 31, 2019

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…

15reactions
DaanAcohencommented, May 12, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Unit testing a controller throwing exception
AspNetCore.Mvc.ModelBinding.BindingInfo because creation unexpectedly failed with exception. Please refer to the inner exception to investigate ...
Read more >
Custom Model Binding in ASP.NET Core
Learn how model binding allows controller actions to work directly with model types in ASP.NET Core.
Read more >
BindingInfo Class (Microsoft.AspNetCore.Mvc.ModelBinding)
Gets or sets a predicate which determines whether or not the model should be bound based on state from the current request. Methods....
Read more >
What's new in ASP.NET Core 7.0
Parameter binding for API controller actions binds parameters through dependency injection when the type is configured as a service.
Read more >
Breaking changes in .NET Core 3.0
If you're migrating to version 3.0 of .NET Core, ASP.NET Core, or EF Core, the breaking changes listed in this article may affect...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found