0

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?

4
  • Do you have any obfuscation in place with R8 or Proguard? Commented Mar 6 at 16:59
  • @tomerpacific No I haven't. At least not intentionally. I checked .csproj and project properties and no such option is enabled. Commented Mar 7 at 8:53
  • I found mention of AutoMapper needing the interpreter. Add this line to one of your <PropertyGroup>s: <UseInterpreter>true</UseInterpreter>. NOTE: That is allegedly only required for AutoMapper on iOS. But given your symptom, it is worth trying. Commented Mar 8 at 23:34
  • @ToolmakerSteve I tried but it didn't help, same error occurs. Also documentation says this shouldn't be enabled in android builds Commented Mar 10 at 9:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.