-
|
AutoMapper doesn't seem to work properly with a .NET 6 iOS MAUI Application. On Android, everything is working as expected. On iOS, it crashes trying to map two types that have an IEnumerable property: e.g. // - - - - - Models
public class MyClass
{
public IEnumerable<int> Values { get; set; }
}
public class MyClassDto
{
public IEnumerable<int> Values { get; set; }
}
// - - - - - AutoMapper Configuration
var config = new MapperConfiguration(cfg => cfg.CreateMap<MyClass, MyClassDto>().ReverseMap());
_mapper = config.CreateMapper();
// - - - - - Mapping Code
var myClass = new MyClass()
{
Values = new List<int> { 1, 2, 3 }
};
// Works on Android, fails on iOS
var myClassDto = _mapper.Map<MyClassDto>(myClass);Sample app to reproduce the issue: https://github.com/busec0/MauiAutomapper. Automapper code is on Does anyone know why the failure is happening on iOS only? Tested with Xamarin.iOS as well, and everything is working as expected there. Stack Trace |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
|
@busec0 Does this still happen if you disable the linker? |
Beta Was this translation helpful? Give feedback.
-
|
Hi, is there anyone in the .NET MAUI iOS team that can look at this issue? |
Beta Was this translation helpful? Give feedback.
-
|
I don't know why this happens with AutoMapper on iOS, but I have found an alternative mapping which seems to work: var config = new MapperConfiguration(cfg => cfg
.CreateMap<MyClass, MyClassDto>()
.ForMember(nameof(MyClass.Values), mce => mce.MapAtRuntime())
.ReverseMap()
.ForMember(nameof(MyClassDto.Values), mce => mce.MapAtRuntime())
);This forces the This will have a slight performance impact, so it might be worth using the default mappings on Android/Windows, and using an #ifdef to use the workaround for iOS. And if you have a lot of |
Beta Was this translation helpful? Give feedback.
-
|
Update: This is now being tracked in dotnet/runtime#79354. In addition to the workaround upthread, you can also avoid this problem by disabling the interpreter. Add The interpreter is disabled by default in Release mode, so Release builds shouldn't run into this issue. |
Beta Was this translation helpful? Give feedback.
Update: This is now being tracked in dotnet/runtime#79354.
In addition to the workaround upthread, you can also avoid this problem by disabling the interpreter. Add
<UseInterpreter>False</UseInterpreter>to the initial PropertyGroup in your .csproj file, and AutoMapper will work just fine. The downside is that C# Hot Reload won't work.The interpreter is disabled by default in Release mode, so Release builds shouldn't run into this issue.