Я не могу найти причину, по которой Automapper не может создать мой класс MailDTO (по моему мнению).
Вот код:
Модель Entity:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Project1.Model;
namespace Project1.Entities
{
[DataContract(Name = "Mail", Namespace = "http://site.com/1.0/Mail")]
public class Mail : Message, Project1.Entities.IMail
{
public Mail() { ... }
public Mail(mail mailValue) { ... }
public Mail(mail mailValue, long UserID, bool includereplies) { ... }
public Mail(mail mailValue, int gmtTimeZone, long UserID) { ... }
long m_MailID;
[DataMember(Order = 0)]
public long MailID
{
get { return m_MailID; }
set { m_MailID = value; }
}
long? m_ParentID;
[DataMember(Order = 1)]
public long? ParentID
{
get { return m_ParentID; }
set { m_ParentID = value; }
}
List<Mail> m_Replies = new List<Mail>();
[DataMember(Order = 2)]
public List<Mail> Replies
{
get { return m_Replies; }
set { m_Replies = value; }
}
}
}
DTO - часть:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace Project1.DTO
{
[DataContract(Name = "MailDTO")]
public class MailDTO
{
[DataMember(Order = 0)]
public long MailID { get; set; }
[DataMember(Order = 1)]
public long? ParentID { get; set; }
[DataMember(Order = 2)]
public string MessageValue { get; set; }
[DataMember(Order = 3)]
public long SourceUser { get; set; }
[DataMember(Order = 4)]
public string CreatedDate { get; set; }
[DataMember(Order = 5)]
public List<long> Recipients { get; set; }
[DataMember(Order = 6)]
public List<MailDTO> Replies { get; set; }
public MailDTO(Project1.Entities.Mail mail)
{
MailID = mail.MailID;
ParentID = mail.ParentID;
MessageValue = mail.MessageValue;
SourceUser = mail.SourceUser.UserID;
CreatedDate = mail.CreatedDate.Value.ToString();
Replies = mail.Replies.Select(item => new MailDTO(item)).ToList();
}
}
}
Клиентская сторона:
[WebInvoke(UriTemplate = "RetrieveMail?mailID={mailID}", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public Project1.DTO.MailDTO RetrieveMail(Project1.Entities.UserSession details, long mailID)
{
Project1.Logic.IMessageComponent svc = Project1.Logic.ConfigAndResourceComponent.Container().Resolve<Project1.Logic.IMessageComponent>();
Project1.Entities.Mail item = svc.RetrieveMail(details, mailID); // Return data is fine, no problem here
Project1.DTO.MailDTO converted = Mapper.Map<Project1.Entities.Mail, Project1.DTO.MailDTO>(item); // The error goes here
return converted;
}
Global.asax.cs:
Mapper.CreateMap<Project1.Entities.Mail, Project1.DTO.MailDTO>();
Mapper.CreateMap<Project1.Entities.Mail, Project1.DTO.MailDTO>().ForMember(dest => dest.SourceUser, opt => opt.MapFrom(src => src.SourceUser.UserID));
Mapper.CreateMap<Project1.Entities.Mail, Project1.DTO.MailDTO>().ForMember(dest => dest.CreatedDate, opt => opt.MapFrom(src => src.CreatedDate.Value.ToString()));
Mapper.CreateMap<Project1.Entities.Mail, Project1.DTO.MailDTO>().ForMember(dest => dest.Recipients, opt => opt.MapFrom(src => src.Recipients.Select(item => item.UserDetail.UserID).ToList()));
Mapper.CreateMap<Project1.Entities.Mail, Project1.DTO.MailDTO>().ForMember(dest => dest.Replies, opt => opt.MapFrom(src => src.Replies.Select( item => new Project1.DTO.MailDTO(src)).ToList()));
Mapper.AssertConfigurationIsValid();
Произошла ошибка:
Type 'Project1.DTO.MailDTO' does not have a default constructor
Trying to map Project1.Entities.Mail to Project1.DTO.MailDTO.
Using mapping configuration for Project1.Entities.Mail to Project1.DTO.MailDTO
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown
at System.Linq.Expressions.Expression.New(Type type)
at AutoMapper.DelegateFactory.<>c__DisplayClass1.<CreateCtor>b__0(Type t)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at AutoMapper.DelegateFactory.CreateCtor(Type type)
at AutoMapper.Mappers.ObjectCreator.CreateObject(Type type)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.CreateObject(ResolutionContext context)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.NewObjectPropertyMapMappingStrategy.GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
Полагаю, проблема в следующем:
public List<MailDTO> Replies { get; set; } // Collects MailID and UserID + other details
До этого было:
public List<long> Replies { get; set; } // Collects MailID
И это работает нормально, но я должен изменить его из-за добавленной информации.
Кто-нибудь может помочь мне решить ошибку? Большое спасибо.