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.

Missing Method Exception (Parameterless Constructor) on ProjectTo()

See original GitHub issue

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().

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:38 (20 by maintainers)

github_iconTop GitHub Comments

2reactions
rmagrudercommented, Jun 30, 2017

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:

Activator.CreateInstance(typeof(System.String)) No parameterless constructor defined for this object.

  • System.RuntimeTypeHandle.CreateInstance(System.RuntimeType, bool, bool, ref bool, ref System.RuntimeMethodHandleInternal, ref bool)
  • System.RuntimeType.CreateInstanceSlow(bool, bool, bool, ref System.Threading.StackCrawlMark)
  • System.Activator.CreateInstance(System.Type, bool)
  • System.Activator.CreateInstance(System.Type)

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.

1reaction
lbargaoanucommented, Jul 3, 2017

Pull request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ProjectTo does not recognize parameterless constructor
Then I want to retrieve data in a query and use ProjectTo extension method of AutoMapper for it: return await _context.Branches .Where(b =>...
Read more >
No parameterless constructor ERROR when i deploy on ...
MissingMethodException : No parameterless constructor defined for this object. Source Error: An unhandled exception was generated during the execution of the ...
Read more >
"No parameterless constructor defined for this object" after ...
I had this error after adding a new project to my Sitecore instance which uses GlassMapper. If you're experiancing the same issue try...
Read more >
No parameterless constructor defined for this object
MissingMethodException : No parameterless constructor defined for this object.< br > at System.RuntimeTypeHandle.
Read more >
March 2020 – What I Broke
MissingMethodException : No parameterless constructor defined for type 'Catalog.Api.Infrastructure.CatalogContext'. at System.RuntimeType.
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