I use Automapper (v.14) in my .Net MAUI (v.9) app. I configure it as per documentation:
mauiAppBuilder.Services.AddAutoMapper(typeof(MappingProfile));
Then I am trying to resolve IMapper object in constructor of ViewModel
public MainPageViewModel(IMapper mapper)
Everything works fine on DEBUG. When I switch to Release, I get error:
[AndroidRuntime] android.runtime.JavaProxyThrowable: [System.Reflection.TargetInvocationException]: Arg_TargetInvocationException
[AndroidRuntime] at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs + 0x58(Unknown Source)
[AndroidRuntime] at System.RuntimeType.CreateInstanceMono + 0xa8(Unknown Source)
[AndroidRuntime] at System.RuntimeType.CreateInstanceOfT + 0x0(Unknown Source)
[AndroidRuntime] at System.Activator.CreateInstance + 0x18(Unknown Source)
[AndroidRuntime] at Microsoft.Extensions.Options.OptionsFactory`1[[AutoMapper.MapperConfigurationExpression, AutoMapper, Version=14.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005]].CreateInstance + 0x0(Unknown Source)
[AndroidRuntime] at Microsoft.Extensions.Options.OptionsFactory`1[[AutoMapper.MapperConfigurationExpression, AutoMapper, Version=14.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005]].Create + 0x0(Unknown Source)
[AndroidRuntime] at Microsoft.Extensions.Options.UnnamedOptionsManager`1[[AutoMapper.MapperConfigurationExpression, AutoMapper, Version=14.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005]].get_Value + 0x54(Unknown Source)
[AndroidRuntime] at Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions+<>c.<AddAutoMapperClasses>b__13_4 + 0x6(Unknown Source)
...
I found that it must be a linking problem because everything works fine when I disabled AOT and trimming on Release builds in .csproj
I tried Preserving Automapper Assembly but it didn't work, the exception still occured.
<ItemGroup>
<TrimmerRootAssembly Include="AutoMapper" RootMode="library" />
</ItemGroup>
Am I missing some important configuration, mixing concepts or something is broken either on maui or automapper side?
<PropertyGroup>s:<UseInterpreter>true</UseInterpreter>. NOTE: That is allegedly only required for AutoMapper on iOS. But given your symptom, it is worth trying.