'System.AccessViolationException' occurred in Unknown Module. Attempted to read or write protected memory
I am getting this error below, after switching from Automapper on a DTO -> DTO mapping. The EF -> DTO mapping works fine, but than I try to go to DTO -> DTO and it gets this error below. I know its hard to actually know what is going on with my code, but I pasted the relevant code to see if you know if I am doing something wrong.
An unhandled exception of type 'System.AccessViolationException' occurred in Unknown Module. Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
DTO-> DTO (errors)
var season = _seasonsService.GetById(MemberId, seasonId.Value);
if (season == null)
throw new ValidationException(new NotFound("Season"));
viewModel.Season = _mapper.Map<SeasonModel, EditMemberSeasonModel>(season);
EF -> DTO (works, until I do DTO -> DTO on the new DTO object)
public SeasonModel GetById(int? memberId, int organizationSeasonId)
{
Expression<Func<Season, object>>[] includes =
{
q => q.MemberSeasons,
q => q.OrganizationSeason.Organization,
q => q.OrganizationSeason.Sport
};
var @where = PredicateBuilder.New<Season>(q => q.OrganizationSeason.Id == organizationSeasonId);
if(memberId.HasValue)
{
where = where.And(q => q.MemberSeasons.Any(m => m.MemberId == memberId) || q.Events.Any(e => e.Members.Any(t => t.MemberId == memberId && t.Active)) || q.OrganizationSeason.Organization.MemberOrganizations.Any(m => m.MemberId == memberId));
}
return Mapper.Map<Season, SeasonModel>(_seasonsRepository.Get(includes, @where));
}
DTO -> DTO Mapping
config.NewConfig<SeasonModel, EditMemberSeasonModel>()
.Map(d => d.ParticipantRegistrationFields, s => s.OrganizationSeason.ParticipantRegistrationFields)
.Map(d => d.CoachRegistrationFields, s => s.OrganizationSeason.CoachRegistrationFields)
.Map(d => d.PlayerRegistrationFields, s => s.OrganizationSeason.PlayerRegistrationFields)
.Map(d => d.TeamMeta, s => s.OrganizationSeason.TeamMeta)
.Map(d => d.PlayerMeta, s => s.OrganizationSeason.PlayerMeta)
.Map(d => d.EnableRegistration, s => s.OrganizationSeason.EnableRegistration)
.Map(d => d.EnableSanctioning, s => s.OrganizationSeason.EnableSanctioning)
.Map(d => d.EnablePlayerWaivers, s => s.OrganizationSeason.EnablePlayerWaivers)
.Map(d => d.PlayerWaiverText, s => s.OrganizationSeason.PlayerWaiverText)
.Map(d => d.RequireSeasonDivision, s => s.OrganizationSeason.RequireSeasonDivision)
.Map(d => d.LockSeasonDivision, s => s.OrganizationSeason.LockSeasonDivision)
.Map(d => d.AgeCutoffDate, s => s.OrganizationSeason.AgeCutoffDate)
.Map(d => d.StartDate, s => s.OrganizationSeason.StartDate)
.Map(d => d.EndDate, s => s.OrganizationSeason.EndDate)
.Map(d => d.SportId, s => s.OrganizationSeason.SportId)
.Map(d => d.SportName, s => s.OrganizationSeason.Sport.Name)
.Ignore(d => d.ParticipantRegistrationFieldsList)
.Ignore(d => d.CoachRegistrationFieldsList)
.Ignore(d => d.PlayerRegistrationFieldsList)
.Ignore(d => d.Sports)
.Ignore(d => d.Sites)
.Ignore(d => d.ParentSeasons)
.Ignore(d => d.Rankings)
.Map(d => d.RankingId, s => s.OrganizationSeason.RankingId)
.Map(d => d.Archive, s => s.OrganizationSeason.Archive)
.Map(d => d.OrganizationName, s => s.OrganizationSeason.Organization.Name)
.Map(d => d.OrganizationId, s => s.OrganizationSeason.OrganizationId)
.Ignore(d => d.Organizations)
.Ignore(d => d.SeasonRequired)
.Ignore(d => d.Default)
.Map(t => t.EndYear, s => s.EndYear.To<string>())
.Map(t => t.StartYear, s => s.StartYear.To<string>()).PreserveReference(true).MaxDepth(2);
So I narrowed it down. I guess Automapper was OK with the DateTime to String mapping, but not Mapster. I had to format correctly to a string. I would recommend Mapster using a better error message than the one that happens on this type of issue and add it to the next build.
Before
.Map(d => d.AgeCutoffDate, s => s.OrganizationSeason.AgeCutoffDate)
.Map(d => d.StartDate, s => s.OrganizationSeason.StartDate)
.Map(d => d.EndDate, s => s.OrganizationSeason.EndDate)
After
.Map(d => d.AgeCutoffDate, s => s.OrganizationSeason.AgeCutoffDate.HasValue ? s.OrganizationSeason.AgeCutoffDate.Value.ToString(Config.Formats.Date.Default) : null)
.Map(d => d.StartDate, s => s.OrganizationSeason.StartDate.HasValue ? s.OrganizationSeason.StartDate.Value.ToString(Config.Formats.Date.Default) : null)
.Map(d => d.EndDate, s => s.OrganizationSeason.EndDate.HasValue ? s.OrganizationSeason.EndDate.Value.ToString(Config.Formats.Date.Default) : null)