Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7575219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:38:00+00:00 2026-05-30T16:38:00+00:00

I cant find the reason why Automapper cannot construct my MailDTO class (for what

  • 0

I cant find the reason why Automapper cannot construct my MailDTO class (for what I think).
Here is the code:

Model 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 – part:

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

Client side:

[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();  

Error encountered:

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)  

I guess the problem is because of this:

public List<MailDTO> Replies { get; set; } // Collects MailID and UserID + other details

Before it was:

public List<long> Replies { get; set; }  // Collects MailID

And it work fine, but I have to change it because of added information.
Anyone can help me solve the error? Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:38:01+00:00Added an answer on May 30, 2026 at 4:38 pm

    Add a default constructor to the MailDTO class

    public MailDTO() { }
    

    AutoMapper needs a default constructor to create an instance first; before it can then copy values into the properties. The only constructor defined on MailDTO in your code is one which takes a Project1.Entities.Mail instance; which AutoMapper doesn’t know how to fire.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cant find one way to get this value (comment) into json using javascript.
I suck at php, and cant find the error here. The script gets 2
I cant find the way to fetch only necessary fields of model in certain
I have a problem once again where I cant find the source code because
ive got a slight problem with Eclipse. For some reason i cant find my
For some reason I can't find a way to get the equivalents of sqlite's
I cant find the way how to strip the useless section of my string
I cant find an expression to evaluate a part of a string. I want
I've tryed mylyn but i cant find that feature, if anyone have test mantis
Dust me selectors is installed but cant find anyway of saving the css minus

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.