AutoMapper: Missing Method Exception (Parameterless Constructor) on ProjectTo()
Regression problem. I updated my .net Core 1.1 service to AutoMapper 6.1 and 6.1.1 and both now fail my tests. Downgrading to 6.0.2 fixes the problem.
I have a Repository, a DTO class, a matching EF class, and I create a method in the repository that looks like this:
public IQueryable<ShipmentDto> GetShipments()
{
try
{
var qry = _context.Shipments.ProjectTo<ShipmentDto>();
return qry;
}
catch (Exception ex)
{
throw ex;
}
}
(I just added the catch for debugging purposes). The code throws an Missing Method exception, explaining that I need a parameterless constructor. I have looked at the DTO, the EF class and there are NO constructors of ANY kind. They are just dumb data classes. And that includes the CarrierService property. Its class is also a dumb simple-type POCO. For giggles, I added a bunch of parameterless constructors to them and it did NOT fix the problem. The problem occurs in a call stack containing Activator.CreateInstance() called during the map from source to destination types.
Again, problem goes away when I simply downgrade AutoMapper back to 6.0.2. So something was introduced in 6.1.0 in the ProjectTo() call chain that breaks.
// Mapper.Initialize or just the CreateMap snippet
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<Shipment, ShipmentDto>()
.ForMember(dest => dest.CarrierService, opt => opt.MapFrom(src => src.CarrierService.ServiceDescription));
CreateMap<ShipmentDto, Shipment>()
.IgnoreAllPropertiesWithAnInaccessibleSetter();
CreateMap<OrderSummaryItem, OrderSummaryItemDto>();
}
}
I did try commenting out the .ForMember line just in case it had something to do with the CarrierService type, but this did NOT make the problem go away.
Version: x.y.z
6.1.0 introduces the problem. Reversion to 6.0.2 makes the tests run fine.
Expected behavior
I expect my repository Dto type to map properly to create an IQueryable I am using in the repository.
Actual behavior
Instead, just creating the query object results in an exception in ProjectTo() down to Activator.CreateInstance().
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 38 (20 by maintainers)
I did debug this against AutoMapper source and reproduced it. The error is happening when an Nullable Int64 type called “StoreNumber” is being mapped to a Dto Type of the same name of type String.
Activator.CreateInstance is trying to create a System.String on line 14 of NullableSourceExpressionBinder.cs.
In C# Immediate this definitely agrees:
But, as the problem didn’t happen before 6.1 and my datatypes didn’t change, did Automapper change the way it created types going from an Int64? to a String?
I pulled 6.0.2 branch and debugged that (which ran without error), but it looks like there was a massive re-write of the mapping resolution code between 6.0.2 and 6.1. I’m now looking at making a simple Int64 Nullable => String mapper repro.
Pull request.