Mapping a List to a larger List error
See original GitHub issueTo start, in a real run of my application this probably wouldn’t happen, but I wanted to understand why this is happening and if there is a workaround.
I have a mapping that is from a List of hourly weather data objects (forecast time, temperature, humidity, cloud cover, etc.). I am mapping to a List of SOAP message objects that can only handle one data type at a time. So I have to create a List of message objects for each weather attribute (temp, humidity, etc.). There can be multiple hours in each message, they just have to correspond to the same attribute. This creates for any size list an output list of 6 (# of weather attributes) message objects.
So, I could be mapping 2 hours of weather forecasts into 6 message objects, where each object contains 2 hours. Obviously, I am going to be getting around 24-48 hours of forecast data, so the source list is much larger than the 6 output. But when unit testing, I am only testing a few hours for quickness. This revealed an error in Automapper. It would appear Automapper makes the assumption that the destination list will be the same size or smaller than the source. Is there way to flag a mapping to not make this assumption?
Thank you for any assistance
Here is the error and the mapping code: Destination property: Capacity Exception of type ‘AutoMapper.AutoMapperMappingException’ was thrown. —> System.ArgumentOutOfRangeException: capacity was less than the current size. Parameter name: value
top of stack trace: System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) System.Collections.Generic.List`1.set_Capacity(Int32 value) SetCapacity(Object , Object ) AutoMapper.Internal.PropertyAccessor.SetValue(Object destination, Object value)
// Don’t worry about syntax, this is a very cut down version of my mapping, but this covers the logic I explained up top // Also, there is no issue with my mapping logic because I pull out everything inside the construct using and run directly in the unit test it works perfect. My assumption is automapper assumes the list is going to get smaller Mapper.CreateMap<List<WeatherInstance>, PowerSystemDataType>() .ConstructUsing(src => { var psds = new PowerSystemDataType(); // collection object of type List<PSD> psds.Add(new PSD(Type=“TempF”, Value=src.Temperature)); psds.Add(new PSD(Type=“Humidity”, Value=src.Humidity)); psds.Add(new PSD(Type=“Cloud Cover”, Value=src.CloudCover)); … more weather attributes return psds; });
Issue Analytics
- State:
- Created 11 years ago
- Comments:8 (2 by maintainers)
Top GitHub Comments
I’m guessing based on this:
…that an earlier part of the mapping or some internal mechanism of PowerSystemDataType is setting the Count of the destination list to 18. Subsequently, when AutoMapper reaches the Capacity property, it’s trying to ratchet it back down to 6, which is illegal:
http://msdn.microsoft.com/en-us/library/y52x03h2.aspx (see Exceptions)
Have you considered
… which ignores the (invalid) source capacity when writing the destination object?
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.