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.

AutoMapper catches and ignores NullReferenceException

See original GitHub issue

As written in:

http://stackoverflow.com/questions/7332426/automapper-catches-and-ignores-nullreferenceexception

Maybe this is by design, but we initially did not expect automapper to catch and ignore all NullReferenceExceptions in our mappings. We mostly use the MapFrom and create sometimes complex expressions. We want these mappings to fail if there’s any exception, even a NullReferenceException, but we cant get AutoMapper to do that. Is there any way to make automapper not ignore all these exceptions without having to write a custom value resolver for every case? This would mean alot of extra code for us, so much in fact that it probably would be less code without using automapper in the first place.

These are the test that we would expect to all pass:

[TestFixture] public class Tests { [SetUp] public void Setup() { Mapper.Reset(); }

[Test]
public void ShouldThrowMappingExceptionUsingMapFromExpression()
{
    Mapper.CreateMap<Source, Destination>()
        .ForMember(d => d.DestinationMember, o => o.MapFrom(s => s.SourceMember.SourceProperty))
        ;

    Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
}

[Test]
public void ShouldThrowMappingExceptionUsingResolveUsingExpression()
{
    Mapper.CreateMap<Source, Destination>()
        .ForMember(d => d.DestinationMember, o => o.ResolveUsing(s => s.SourceMember.SourceProperty))
        ;

    Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
}

[Test]
public void ShouldThrowMappingExceptionUsingResolverInstance()
{
    Mapper.CreateMap<Source, Destination>()
        .ForMember(d => d.DestinationMember, o => o.ResolveUsing(new TestValueResolver()).FromMember(s => s.SourceMember))
        ;

    Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
}

[Test]
public void ShouldThrowMappingExceptionUsingResolverType()
{
    Mapper.CreateMap<Source, Destination>()
        .ForMember(d => d.DestinationMember, o => o.ResolveUsing<TestValueResolver>().FromMember(s => s.SourceMember))
        ;

    Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
}

}

public class Destination { public string DestinationMember { get; set; } }

public class Source { public SourceChild SourceMember { get; set; } }

public class SourceChild { public string SourceProperty { get; set; } }

public class TestValueResolver : ValueResolver<SourceChild, string> { protected override string ResolveCore(SourceChild source) { return source.SourceProperty; } }

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:20 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
jacob-ewaldcommented, Apr 15, 2019

Ah, sorry, I see what happened. When reading the docs it seemed like all one had to do was replace ResolveUsing with MapFrom, but that’s only part of it. You also need to switch how you’re passing arguments to it if you have lambdas. I see that now in the documentation, thanks @lbargaoanu.

0reactions
lock[bot]commented, May 16, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Automapper custom resolver throws null exception in ...
Hi, We are using AutoMapper in our Commerce site. While mapping from the custom model to an EPiServer Model(VariationContent) throws a below error....
Read more >
How come visual studio can't display the name of ...
Today I worked with Automapper and got instantly furstrated when it wasn't able to tell me which property it wasn't able to map...
Read more >
AutoMapper Ignore Method in C# with Examples
In this article, I am going to discuss how to use the AutoMapper Ignore Method in C# with Examples. AutoMapper tries to Map...
Read more >
The reasons behind why I don't use AutoMapper.
The first problem is that static analysis starts to report that some fields from my entity are never used. There is no direct...
Read more >
Jimmy Bogard
AutoMapper will ignore null reference exceptions when mapping your source to your target. This is by design. If you.
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