2016-05-05 2 views
0

本当に簡単なマッピングでAutomapperを試してみましたが、うまくいきません。 私は別のタイプのClaimItemへSystem.Security.Claims.Claimタイプにマップしようとしています:Automapper -AutoMapper.AutoMapperMappingException

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

をしかし、私は常に取得:

AutoMapper.AutoMapperMappingException:行方不明の型マップの設定またはサポートされていないマッピングを。

マッピングタイプ:クレーム - > ClaimItem System.Security.Claims.Claim - > CommonAuth.ClaimItem

先のパス:ClaimItem

ソース値: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth: 2016年5月5日

これは私の設定です:

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

を作成したことを意味間違っているものを、テキストの説明がなければなりません。あなたはすべてのテキストを提供することができます –

+0

更新! :-)私の宛先タイプでは、TypeとValueという2つの小道具しかなく、これらの2つのプロパティをsourceTypeクレームからマッピングする必要があります。 ソースと宛先に同じ名前があります。 – Legends

+0

マッパーはどこにありますか?バージョン4で注入する必要があるからです。 –

答えて

2

the documentationからは、configからマッパーを作成する必要があります。ですから、この

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; 
     } 
    } 

のようなあなたのコードをなめらかに持っている必要があります。これは、あなたが静的マッパーそれをhouldが設定から作成されている場合は、あなたが

+0

これで動作します。だから、「マッパー」にアクセスするためにシングルトンを作成する必要があります。 – Legends

+0

あなたは答えを更新しました。ありがとうございます。 – Legends

+0

@Legends私は自分の答えを更新しています。それはそれに依存します。シングルトンを使うこともできますし、マッパーを作るファクトリクラスを持つこともできます。 –