Search code examples
iosentity-framework-corelinker.net-6.0maui

What trimming/linker settings are needed for Entity Framework Core in .NET MAUI on iOS?


I'm migrating a Xamarin Forms 5.0 app, using Entity Framework Core 3.1 and a SQLite database to .NET MAUI 6.0 and Entity Framework Core 6.0. The Debug build works fine, but when I do a Release build, the app crashes while starting up.

The easiest way to reproduce it on macOS is to use the .NET MAUI App template in Visual Studio, edit the project file and add some configuration to make sure you can use dotnet build (Release builds from Visual Studio for Mac don't work at the moment):

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0-ios|AnyCPU'">
      <CreatePackage>false</CreatePackage>
      <CodesignProvision>Automatic</CodesignProvision>
      <CodesignKey>iPhone Developer</CodesignKey>
      <RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net6.0-ios|AnyCPU'">
      <CreatePackage>false</CreatePackage>
      <CodesignProvision>Automatic</CodesignProvision>
      <CodesignKey>iPhone Distribution</CodesignKey>
      <RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
    </PropertyGroup>

Open the project folder in Terminal, and run

dotnet build MauiEFCore.csproj -c Release -f net6.0-ios -t:Run -p:_DeviceName=<Device UDID>

(be sure to specify the right project file, and the UDID of your iPhone). This works, but as soon as you add Entity Framework Core to the project:

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.11" />
    <PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.3" />
  </ItemGroup>

the app starts to crash on startup. The error about Microsoft.Maui.Graphics seems unrelated - that's just a standard Maui module, right? I could not find more information in the Console app either.

 Launched application 'com.companyname.mauiefcore' on 'Gerwin's iPhone 11' with pid 25752
2023-01-06 16:00:53.767 MauiEFCore[25752:10999573] error: Failed to load AOT module 'Microsoft.Maui.Graphics' while running in aot-only mode because a dependency cannot be found or it is out of date.

=================================================================
    Native Crash Reporting
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
    Native stacktrace:
=================================================================
    0x104228540 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode
    0x104211788 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode
    0x104227d28 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode
    0x1f2ed0a90 - /usr/lib/system/libsystem_platform.dylib : <redacted>
    0x1f2f6c1ac - /usr/lib/system/libsystem_pthread.dylib : pthread_kill
    0x1ac554c8c - /usr/lib/system/libsystem_c.dylib : abort
    0x103f6d5d4 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : xamarin_find_protocol_wrapper_type
    0x104264d10 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode
    0x104264e44 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode
    0x104200664 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode
    0x1040c9ea8 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode
    0x1040c9900 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode
    0x1040cb5bc - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode
    0x103f6cbf8 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : xamarin_get_block_descriptor
    0x10426cb64 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode
    0x103f7685c - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : xamarin_log
    0x10426cd04 - /private/var/containers/Bundle/Application/F14C623C-8C03-4845-94D8-C0B1EFBA07A6/PublicStaging.app/MauiEFCore : _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode
    0x1c3776960 - /usr/lib/dyld : <redacted>

=================================================================
    Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x1e284f160):0x1e284f150  ff 0f 5f d6 c0 03 5f d6 10 29 80 d2 01 10 00 d4  .._..._..)......
0x1e284f160  03 01 00 54 7f 23 03 d5 fd 7b bf a9 fd 03 00 91  ...T.#...{......
0x1e284f170  8e ed ff 97 bf 03 00 91 fd 7b c1 a8 ff 0f 5f d6  .........{...._.
0x1e284f180  c0 03 5f d6 7f 23 03 d5 fd 7b bf a9 fd 03 00 91  .._..#...{......

=================================================================
    Managed Stacktrace:
=================================================================
=================================================================
  Application 'com.companyname.mauiefcore' terminated (with exit code '' and/or crashing signal '6).

Build succeeded.

Because the Debug build continues to work, I suspect linking is to blame; however, disabling linking (equivalent to adding <MtouchLink>None</MtouchLink> to the project file) has no effect. I still see these lines in the build output:

Optimizing assemblies for size may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
Optimizing assemblies for size. This process might take a while.

That link leads to a page about trimming, which is unexpected since I don't have trimming enabled, but apparently it's automatic, and switching it off leads to another error:

/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/16.1.229/targets/Xamarin.Shared.Sdk.targets(269,3): error : iOS projects must build with PublishTrimmed=true. Current value: false.

In my Xamarin Forms project I had added --linkskip System.Core to the 'Additional mtouch arguments' in the 'iOS Build options' as described here but that doesn't help - the detailed build output says it's ignored:

       Task "ParseBundlerArguments"
         Skipping unknown argument 'linkskip' with value ''
       Done executing task "ParseBundlerArguments".

What do I need to do in order to make Entity Framework Core work in Release mode?

I have tried adding the linker settings as described in this answer for EF5, even added some other assemblies to that list (Microsoft.EntityFrameworkCore.Abstractions and SQLitePCLRaw.provider.internal) but to no avail.

FWIW, for Android, Release builds work fine.


Solution

  • As GitHub user @Ghevi noted here, using the interpreter by setting the MSBuild property UseInterpreter in the project file did the job:

        <PropertyGroup Condition="'$(TargetFramework)' == 'net6.0-ios'">
          <UseInterpreter>True</UseInterpreter>
        </PropertyGroup>