我正在将使用 Entity Framework Core 3.1 和 SQLite 数据库的 Xamarin Forms 5.0 应用程序迁移到 .NET MAUI 6.0 和 Entity Framework Core 6.0。调试构建工作正常,但是当我进行发布构建时,应用程序在启动时崩溃。
在 macOS 上重现它的最简单方法是在 Visual Studio 中使用 .NET MAUI App 模板,编辑项目文件并添加一些配置以确保您可以使用dotnet build
(Visual Studio for Mac 的发布版本目前无法运行 https://developercommunity.visualstudio.com/t/Building-in-Release-mode-for-iOS-results/10235007):
<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>
在终端中打开项目文件夹,然后运行
dotnet build MauiEFCore.csproj -c Release -f net6.0-ios -t:Run -p:_DeviceName=<Device UDID>
(请务必指定正确的项目文件和 iPhone 的 UDID)。这是可行的,但是一旦您将 Entity Framework Core 添加到项目中:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.11" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.3" />
</ItemGroup>
该应用程序在启动时开始崩溃。有关 Microsoft.Maui.Graphics 的错误似乎无关 - 这只是一个标准的 Maui 模块,对吗?我也无法在控制台应用程序中找到更多信息。
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.
因为调试版本继续工作,我怀疑链接是罪魁祸首;然而,禁用链接 https://i.stack.imgur.com/Ih0Mw.png(相当于添加<MtouchLink>None</MtouchLink>
到项目文件)没有效果。我仍然在构建输出中看到这些行:
优化程序集的大小可能会改变应用程序的行为。发布后一定要进行测试。看:https://aka.ms/dotnet-illink https://aka.ms/dotnet-illink
优化装配体的尺寸。这个过程可能需要一段时间。
该链接指向一个有关的页面trimming,这是出乎意料的,因为我没有启用修剪 https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options?pivots=dotnet-6-0#enable-trimming,但显然它是自动的,关闭它会导致另一个错误:
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/16.1.229/targets/Xamarin.Shared.Sdk.targets(269,3):错误:iOS 项目必须使用 PublishTrimmed=true 进行构建。当前值:假。
在我的 Xamarin Forms 项目中我添加了--linkskip System.Core
到“iOS 构建选项”中的“其他 mtouch 参数”,如所述here https://learn.microsoft.com/en-us/ef/core/get-started/xamarin#explore-the-code但这并没有帮助 - 详细的构建输出表明它被忽略了:
Task "ParseBundlerArguments"
Skipping unknown argument 'linkskip' with value ''
Done executing task "ParseBundlerArguments".
为了使 Entity Framework Core 在发布模式下工作,我需要做什么?
我已尝试添加链接器设置,如中所述这个答案 https://stackoverflow.com/a/72619246/4751173对于 EF5,甚至在该列表中添加了一些其他程序集(Microsoft.EntityFrameworkCore.Abstractions
and SQLitePCLRaw.provider.internal
)但无济于事。
FWIW,对于 Android,发布版本工作正常。