0
votes

I trying out Automapper, with a really easy mapping, but it does not work. I am trying to map a System.Security.Claims.Claim type to another type ClaimItem:

public class ClaimItem
{
    public string Type { get; set; }
    public string Value { get; set; }
}

But I always get:

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types: Claim -> ClaimItem System.Security.Claims.Claim -> CommonAuth.ClaimItem

Destination path: ClaimItem

Source value: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth: 05.05.2016

Here is my configuration:

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<Claim, ClaimItem>(MemberList.Destination);
});

config.AssertConfigurationIsValid();

var cls = getClaims();
List<ClaimItem> list = new List<ClaimItem>();
cls.ForEach(cl => list.Add(Mapper.Map<ClaimItem>(cl)));
1
There must be text explanation what is wrong. Can you provide all textVova Bilyachat
Updated! :-) In my destination type I have only two props, Type and Value, I want these two properties to be mapped from the sourceType Claims. They have the same names in source and destination.Legends
Where you get Mapper? because it should injected in version 4Vova Bilyachat
This is just a Unit test, where I run the code for testing. I thought Mapper is a wrapper, which has a reference to the config, right???Legends

1 Answers

2
votes

From the documentation you must create mapper from config. So you should have in your code smth like this

 private static Mapper _mapper;
    public static Mapper Mapper
    {
        get
        {
            if (_mapper == null)
            {
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<Claim, ClaimItem>(MemberList.Destination);
                });

                config.AssertConfigurationIsValid();
                _mapper = config.CreateMapper();
            }
            return _mapper;
        }
    }

This means that if you have static Mapper it hould be created from config which you create