5

I know a lot of questions have been asked regarding this topic, but none of them provided an answer for my problem. That's why I'm creating a new question.

I've looked on google and here for answers and have found some which have improved my setup of AutoMapper. (i.e. only creating your mappings only once)

My problem is that I sometimes get the exception and sometimes I don't. And I have absolutely no idea how to solve it. I've been searching a whole day now and I can't find anything.

When I run Mapper.AssertConfigurationIsValid();it succeeds.

I've tried to add the following:

Mapper.Configuration.AllowNullDestinationValues = true;
Mapper.AllowNullDestinationValues = true;  

And still I get errors :s

My setup is as follows:

In my global.asax file I call AutoMapperSetup.SetupAutoMapper().

protected void Application_Start(object sender, EventArgs e)
    {
        ...
        AutoMapperSetup.SetupAutoMapper();
    }

The AutoMapperSetup class is described below: (I do know that my static constructor is getting executed. If I put a breakpoint, it's getting hit. So that code does get executed)

public static class AutoMapperSetup
{

static AutoMapperSetup()
    {
        #region GetPlanning
        Mapper.CreateMap<GetPlanningResult_v3, FWSGetPlanningResult>()
            .ForSourceMember(x => x.ItemSelection, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<ExecutionResult, FWSExecutionResult>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<ExecutionResult.Error, FWSExecutionResult.FWSError>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<ExecutionResult.Warning, FWSExecutionResult.FWSWarning>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<GetPlanningHistoricalSelectionResult_v3, FWSGetPlanningHistoricalSelectionResult>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<PlanningResult_v3, FWSPlanningResult>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Anomaly, FWSAnomaly>()
            .ForSourceMember(x => x.AnomalyID, y => y.Ignore())
            .ForSourceMember(x => x.Dispatcher, y => y.Ignore())
            .ForSourceMember(x => x.IsDealed, y => y.Ignore())
            .ForSourceMember(x => x.AnomalyDateTime, y => y.Ignore())
            .ForSourceMember(x => x.Trip, y => y.Ignore())
            .ForSourceMember(x => x.Place, y => y.Ignore())
            .ForSourceMember(x => x.Job, y => y.Ignore())
            .ForSourceMember(x => x.Product, y => y.Ignore())
            .ForSourceMember(x => x.Vehicle, y => y.Ignore())
            .ForSourceMember(x => x.Driver, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<CommentHistoricalResult, FWSCommentHistoricalResult>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));

        Mapper.CreateMap<IdentifierResult, FWSIdentifierResult>()
            .ForSourceMember(x => x.Code, y => y.Ignore())
            .ForSourceMember(x => x.ID, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<IdentifierPerson, FWSIdentifierPerson>()
            .ForSourceMember(x => x.LastName, y => y.Ignore())
            .ForSourceMember(x => x.FirstName, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<IdentifierVehicleResult, FWSIdentifierVehicleResult>()
            .ForSourceMember(x => x.LicensePlate, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<TripResult_v3, FWSTripResult>()
            .ForSourceMember(x => x.ModificationDate, y => y.Ignore())
            .ForSourceMember(x => x.ModificationID, y => y.Ignore())
            .ForSourceMember(x => x.ModificationNumber, y => y.Ignore())
            .ForSourceMember(x => x.EndDate, y => y.Ignore())
            .ForSourceMember(x => x.ExternalSynchStatus, y => y.Ignore())
            .ForSourceMember(x => x.CreatedByDriver, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Trip, FWSTrip>()
            .ForSourceMember(x => x.References, y => y.Ignore())
            .ForSourceMember(x => x.TypeCustom, y => y.Ignore())
            .ForSourceMember(x => x.CustomNr, y => y.Ignore())
            .ForSourceMember(x => x.StartTripAct, y => y.Ignore())
            .ForSourceMember(x => x.StopTripAct, y => y.Ignore())
            .ForSourceMember(x => x.OrderSeq, y => y.Ignore())
            .ForSourceMember(x => x.Overwrite, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<PlaceResult_v2, FWSPlaceResult>()
            .ForSourceMember(x => x.ModificationDate, y => y.Ignore())
            .ForSourceMember(x => x.ModificationID, y => y.Ignore())
            .ForSourceMember(x => x.ModificationNumber, y => y.Ignore())
            .ForSourceMember(x => x.SessionID, y => y.Ignore())
            .ForSourceMember(x => x.Export, y => y.Ignore())
            .ForSourceMember(x => x.ReadDateTime, y => y.Ignore())
            .ForSourceMember(x => x.Format, y => y.Ignore())
            .ForSourceMember(x => x.Documents, y => y.Ignore())
            .ForSourceMember(x => x.TachoActivities, y => y.Ignore())
            .ForSourceMember(x => x.EndDate, y => y.Ignore())
            .ForSourceMember(x => x.DriverInfo, y => y.Ignore())
            .ForSourceMember(x => x.AlarmDateTime, y => y.Ignore())
            .ForSourceMember(x => x.ExternalSynchStatus, y => y.Ignore())
            .ForSourceMember(x => x.CreatedByDriver, y => y.Ignore())
            .ForSourceMember(x => x.Comments, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Place, FWSPlace>()
            .ForSourceMember(x => x.References, y => y.Ignore())
            .ForSourceMember(x => x.CustomNr, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Consultation, FWSConsultation>()
            .ForSourceMember(x => x.ModificationDate, y => y.Ignore())
            .ForSourceMember(x => x.ConsultationID, y => y.Ignore())
            .ForSourceMember(x => x.Vehicle, y => y.Ignore())
            .ForSourceMember(x => x.Driver, y => y.Ignore())
            .ForSourceMember(x => x.Position, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<ExtraInfo, FWSExtraInfo>()
            .ForSourceMember(x => x.Date, y => y.Ignore())
            .ForSourceMember(x => x.InfoID, y => y.Ignore())
            .ForSourceMember(x => x.Export, y => y.Ignore())
            .ForSourceMember(x => x.Activity, y => y.Ignore())
            .ForSourceMember(x => x.Vehicle, y => y.Ignore())
            .ForSourceMember(x => x.Driver, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Activity, FWSActivity>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<PlaceIdentifier, FWSPlaceIdentifier>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<TripIdentifier, FWSTripIdentifier>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));

        Mapper.CreateMap<Position, FWSPosition>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<PlanningItem, FWSPlanningItem>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<GeofencingResult, FWSGeofencingResult>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Geofencing, FWSGeofencing>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Comment, FWSComment>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<JobResult_v2, FWSJobResult>()
            .ForSourceMember(x => x.Author, y => y.Ignore())
            .ForSourceMember(x => x.Export, y => y.Ignore())
            .ForSourceMember(x => x.CreatedbyDriver, y => y.Ignore())
            .ForSourceMember(x => x.ExternalSynchStatus, y => y.Ignore())
            .ForSourceMember(x => x.Transfer, y => y.Ignore())
            .ForSourceMember(x => x.CancelStatus, y => y.Ignore())
            .ForSourceMember(x => x.ModificationDate, y => y.Ignore())
            .ForSourceMember(x => x.ModificationID, y => y.Ignore())
            .ForSourceMember(x => x.ModificationNumber, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<ProductResult_v2, FWSProductResult>()
            .ForSourceMember(x => x.ModificationDate, y => y.Ignore())
            .ForSourceMember(x => x.ModificationID, y => y.Ignore())
            .ForSourceMember(x => x.ModificationNumber, y => y.Ignore())
            .ForSourceMember(x => x.Format, y => y.Ignore())
            .ForSourceMember(x => x.ProductType, y => y.Ignore())
            .ForSourceMember(x => x.Export, y => y.Ignore())
            .ForSourceMember(x => x.Transfer, y => y.Ignore())
            .ForSourceMember(x => x.ExternalSynchStatus, y => y.Ignore())
            .ForSourceMember(x => x.CreatedByDriver, y => y.Ignore())
            .ForSourceMember(x => x.TnrPlace, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Job, FWSJob>()
            .ForSourceMember(x => x.OrderSeq, y => y.Ignore())
            .ForSourceMember(x => x.DriverDisplay, y => y.Ignore())
            .ForSourceMember(x => x.Comment, y => y.Ignore())
            .ForSourceMember(x => x.CustomNr, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Product, FWSProduct>()
            .ForSourceMember(x => x.References, y => y.Ignore())
            .ForSourceMember(x => x.CustomNr, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<ViaRouteItemResult, FWSViaRouteItemResult>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Customer, FWSCustomer>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Shipper, FWSShipper>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<ActivityPlace, FWSActivityPlace>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<Transfer, FWSTransfer>()
            .ForSourceMember(x => x.SendDateTime, y => y.Ignore())
            .ForSourceMember(x => x.TransferCancelID, y => y.Ignore())
            .ForSourceMember(x => x.TransferDateTime, y => y.Ignore())
            .ForSourceMember(x => x.TransferID, y => y.Ignore())
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));

        Mapper.CreateMap<enumPlanningStatus, FWSEnumPlanningStatus>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<enumSynchronisationStatus, FWSEnumSynchronisationStatus>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<enumCancelStatus, FWSEnumCancelStatus>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<enumTransferStatus, FWSEnumTransferStatus>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<enumGeoFencingEventType, FWSenumGeoFencingEventType>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        Mapper.CreateMap<enumPositionFormat, FWSenumPositionFormat>()
            .ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
        #endregion

        #region InsertUpdatePlanning
        Mapper.CreateMap<FWSPlanningInsert, PlanningInsert>();
        Mapper.CreateMap<FWSPlanning, Planning>();
        Mapper.CreateMap<FWSTripInsert, TripInsert>()
            .ForMember(x => x.References, y => y.Ignore())
            .ForMember(x => x.TypeCustom, y => y.Ignore())
            .ForMember(x => x.CustomNr, y => y.Ignore())
            .ForMember(x => x.StartTripAct, y => y.Ignore())
            .ForMember(x => x.StopTripAct, y => y.Ignore())
            .ForMember(x => x.OrderSeq, y => y.Ignore())
            .ForMember(x => x.Overwrite, y => y.Ignore());
        Mapper.CreateMap<FWSTrip, Trip>()
            .ForMember(x => x.References, y => y.Ignore())
            .ForMember(x => x.TypeCustom, y => y.Ignore())
            .ForMember(x => x.CustomNr, y => y.Ignore())
            .ForMember(x => x.StartTripAct, y => y.Ignore())
            .ForMember(x => x.StopTripAct, y => y.Ignore())
            .ForMember(x => x.OrderSeq, y => y.Ignore())
            .ForMember(x => x.Overwrite, y => y.Ignore());
        Mapper.CreateMap<FWSPlaceInsert, PlaceInsert>()
            .ForMember(x => x.References, y => y.Ignore())
            .ForMember(x => x.CustomNr, y => y.Ignore());
        Mapper.CreateMap<FWSPlace, Place>()
            .ForMember(x => x.References, y => y.Ignore())
            .ForMember(x => x.CustomNr, y => y.Ignore());
        Mapper.CreateMap<FWSJobInsert, JobInsert>()
            .ForMember(x => x.OrderSeq, y => y.Ignore())
            .ForMember(x => x.DriverDisplay, y => y.Ignore())
            .ForMember(x => x.Comment, y => y.Ignore())
            .ForMember(x => x.CustomNr, y => y.Ignore());
        Mapper.CreateMap<FWSJob, Job>()
            .ForMember(x => x.OrderSeq, y => y.Ignore())
            .ForMember(x => x.DriverDisplay, y => y.Ignore())
            .ForMember(x => x.Comment, y => y.Ignore())
            .ForMember(x => x.CustomNr, y => y.Ignore());
        Mapper.CreateMap<FWSProductInsert, ProductInsert>()
            .ForMember(x => x.References, y => y.Ignore())
            .ForMember(x => x.CustomNr, y => y.Ignore());
        Mapper.CreateMap<FWSProduct, Product>()
            .ForMember(x => x.References, y => y.Ignore())
            .ForMember(x => x.CustomNr, y => y.Ignore());
        Mapper.CreateMap<FWSIdentifier, Identifier>();
        Mapper.CreateMap<FWSIdentifierVehicle, IdentifierVehicle>();
        Mapper.CreateMap<FWSIdentifierSite, IdentifierSite>();
        Mapper.CreateMap<FWSenumIdentifierType, enumIdentifierType>();
        Mapper.CreateMap<FWSenumIdentifierVehicleType, enumIdentifierVehicleType>();
        Mapper.CreateMap<FWSenumIdentifierSiteType, enumIdentifierSiteType>();
        Mapper.CreateMap<FWSReference, Reference>();
        Mapper.CreateMap<FWSActivity, Activity>();
        Mapper.CreateMap<FWSGeofencing, Geofencing>();
        Mapper.CreateMap<FWSPosition, Position>();
        Mapper.CreateMap<FWSIdentifierViaRoute, IdentifierViaRoute>();
        Mapper.CreateMap<FWSViaPointInsert, ViaPointInsert>();
        Mapper.CreateMap<FWSViaPoint, BusinessFramework.Data.ViaRoute.ViaPoint>();
        Mapper.CreateMap<FWSViaRouteInsert, ViaRouteInsert>();
        Mapper.CreateMap<FWSViaRoute, ViaRoute>();
        Mapper.CreateMap<FWSActivityPlace, ActivityPlace>();
        Mapper.CreateMap<FWSenumGeoFencingEventType, enumGeoFencingEventType>();
        Mapper.CreateMap<FWSenumPositionFormat, enumPositionFormat>();
        Mapper.CreateMap<FWSReferenceProduct, ReferenceProduct>();
        Mapper.CreateMap<FWSenumIdentifierViaRouteType, enumIdentifierViaRouteType>();
        Mapper.CreateMap<FWSenumViaRouteType, enumViaRouteType>();
        Mapper.CreateMap<FWSenumViaRoutePlanningType, enumViaRoutePlanningType>();
        Mapper.CreateMap<FWSIdentifierAddress, IdentifierAddress>();
        Mapper.CreateMap<FWSenumIdentifierAddressType, enumIdentifierAddressType>();
        Mapper.CreateMap<FWSCustomer, Customer>();
        Mapper.CreateMap<FWSShipper, Shipper>();
        #endregion

        #region InsertUpdateSettings
        Mapper.CreateMap<FWSUpdateInsertSettings, UpdateInsertSettings>();
        #endregion

        #region Cancel Planning
        Mapper.CreateMap<FWSPlanningItemSelection, PlanningItemSelection>();
        #endregion
    }

public static void SetupAutoMapper()
    {

    }
}

This gets called when I start my WebServices. The next step is to get the data from DB and map this to my DTO:

FWSGetPlanningResult resultToReturn = Mapper.Map<GetPlanningResult_v3, FWSGetPlanningResult>(result);

Executing the above line of code is giving me the mapping exception error. Below the details of the exception:

AutoMapper.AutoMapperMappingException occurred
HResult=-2146233088
Message=Missing type map configuration or unsupported mapping.

Mapping types:
GetPlanningResult_v3 -> FWSGetPlanningResult
BusinessFramework.Data.Planning.Result.GetPlanningResult_v3 -> BusinessFramework.Data.Planning.FWSGetPlanningResult

Destination path:
FWSGetPlanningResult

Source value:
BusinessFramework.Data.Planning.Result.GetPlanningResult_v3
Source=AutoMapper
StackTrace:
   at Desktop.Web.Fleet.DBFactory.ConvertFromGetPlanningResult(GetPlanningResult_v3 result) in c:\TFSProjects\Tx-Framework\Framework\Main\Source\BusinessFramework\Services.Fleet\Classes\DB\Factories\Planning.cs:line 593
InnerException: 

I hope that someone might give me the solution.. As you can see on the number of objects.. I'm not that eager to start mapping everything manually.

Thanks in advance!

JeremyVm
  • 385
  • 2
  • 3
  • 12
  • Can you post the code that calls `AutoMapperSetup` - it almost looks like the configuration hasn't been initialised correctly. – Mightymuke Jan 09 '13 at 19:45
  • Hi, I've edited my post and have added the full AutoMapperSetup class. I've also posted the Application_Start method from global.asax which calls the AutoMapperSetup code. – JeremyVm Jan 10 '13 at 07:04
  • maybe FWSGetPlanningResult contains some collection AutoMapper doesn't know how to create or map. – Dirk Trilsbeek Jan 10 '13 at 07:22
  • Is that code correct? You're calling `SetupAutoMapper` which is an empty method. The mappings are in `AutoMapperSetup`. ie - you're not instantiating `AutoMapperSetup`, but are calling its empty static method. – Mightymuke Jan 10 '13 at 07:29
  • 1
    @Mightymuke: It's a static constructor. A static constructor gets called the first time the class is referenced. So if I call that method, then my static constructor gets executed first. I can put the code in the method I'm calling, but that would not do much help. – JeremyVm Jan 10 '13 at 08:18
  • 1
    @DirkTrilsbeek: Would AssertConfiguration not see this? Normally this should not be an issue as it's sometimes possible to view the information. – JeremyVm Jan 10 '13 at 08:19
  • Yep - my bad, wasn't thinking. (In my defence, the code is a little obscure :) ). Sorry. – Mightymuke Jan 10 '13 at 08:27
  • My simple unit tests pass OK with your setup. Any chance `GetPlanningResult_v3` or `FWSGetPlanningResult` in the mapping are from different namespaces than `BusinessFramework.Data.Planning.Result.GetPlanningResult_v3` or `BusinessFramework.Data.Planning.FWSGetPlanningResult` – Mightymuke Jan 10 '13 at 08:40
  • Just checked again. Those are the correct namespaces. – JeremyVm Jan 10 '13 at 08:55
  • Then sorry, I have no idea going from the provided information. The only thing I can suggest is to break the mapping (ie, remove one of the ignores or something), then confirm that `AssertConfigurationIsValid` fails (ie, red - green - refactor) – Mightymuke Jan 10 '13 at 09:06
  • Thanks for your help anyway :) I'll have a look at your suggestion and see what it does. – JeremyVm Jan 10 '13 at 09:09
  • That's assuming of course that you're calling it from `Application_Start` rather than a unit test (ie, to ensure that the mapping is actually being configured in your web service). Also if the 2 classes are non-trivial, it might pay to also post those if possible. – Mightymuke Jan 10 '13 at 09:14
  • Also minor point, not sure you need those `ForSourceMember` ignores. [Automapper doesn't work that way. Its only concerned about properties on the destination object.](http://stackoverflow.com/q/4052579/1505426) – Mightymuke Jan 10 '13 at 09:52

1 Answers1

3

My problem has been fixed. The problem was the following:

In our solution we recently started working with IOC. Someone created a TypeAdapter with AutoMapperTypeAdapter as referenced entity.

When in our application a certain module was open or entered, Automapper was being initialized again and my mappings created in global.asax were removed.

After modifying my code to also work using the TypeAdapter, my problem was fixed.

Thanks for everying trying to help me!

JeremyVm
  • 385
  • 2
  • 3
  • 12
  • Do you known in which cases the AutoMapper is initialized? – user823959 Apr 19 '13 at 10:05
  • @JeremyVm Hi!! Can you tell me how you configure automapper at statup project at once. can u add simple example here that would be help ful to me to understand. and i am using asp.net MVC framework. Did you use profile method or generic pattern for automapper. – Sujay Sep 28 '19 at 15:10
  • Hi Tom, That's a bit difficult. It's been almost 7 years since I asked this question, in the meantime I changed companies. So don't know how I've set things up. Also, AutoMapper has had updates in the meantime, so this question is no longer relevant to the current situation. – JeremyVm Sep 30 '19 at 15:26