SQLitePCL.raw: Problem with using Visual Studio for Mac

Issue

Crash when running .NET MAUI application on macOS.

Unhandled Exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exception.
 ---> System.DllNotFoundException: sqlite3
   at SQLitePCL.SQLite3Provider_sqlite3.SQLitePCL.ISQLite3Provider.sqlite3_libversion_number()
   at SQLitePCL.raw.SetProvider(ISQLite3Provider imp)
   at SQLitePCL.Batteries_V2.Init()
   at SQLite.SQLiteConnection..cctor()
   --- End of inner exception stack trace ---
   at SQLiteMAUITest.MainPage..ctor() in /Users/bradmoore/SQLiteMAUITest/MainPage.xaml.cs:line 16
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, Object[] parameters, Boolean wrapExceptions)
   --- End of inner exception stack trace ---
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, Object[] parameters, Boolean wrapExceptions)
   at System.RuntimeType.CreateInstanceMono(Boolean nonPublic, Boolean wrapExceptions)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.Maui.Controls.ShellContent.<>c__DisplayClass19_0.<Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent>b__0()
   at Microsoft.Maui.Controls.ElementTemplate.CreateContent()
   at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(DataTemplate self, Object item, BindableObject container)
   at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.LoadRenderers()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.ViewDidLoad()
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
   at SQLiteMAUITest.Program.Main(String[] args) in /Users/bradmoore/SQLiteMAUITest/Platforms/MacCatalyst/Program.cs:line 13
2022-05-25 11:52:32.989 SQLiteMAUITest[6111:133232] Unhandled managed exception: Exception has been thrown by the target of an invocation. (System.Reflection.TargetInvocationException)
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, Object[] parameters, Boolean wrapExceptions)
   at System.RuntimeType.CreateInstanceMono(Boolean nonPublic, Boolean wrapExceptions)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.Maui.Controls.ShellContent.<>c__DisplayClass19_0.<Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent>b__0()
   at Microsoft.Maui.Controls.ElementTemplate.CreateContent()
   at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(DataTemplate self, Object item, BindableObject container)
   at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.LoadRenderers()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.ViewDidLoad()
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
   at SQLiteMAUITest.Program.Main(String[] args) in /Users/bradmoore/SQLiteMAUITest/Platforms/MacCatalyst/Program.cs:line 13
The type initializer for 'SQLite.SQLiteConnection' threw an exception. (System.TypeInitializationException)
   at SQLiteMAUITest.MainPage..ctor() in /Users/bradmoore/SQLiteMAUITest/MainPage.xaml.cs:line 16
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, Object[] parameters, Boolean wrapExceptions)
sqlite3 (System.DllNotFoundException)
   at SQLitePCL.SQLite3Provider_sqlite3.SQLitePCL.ISQLite3Provider.sqlite3_libversion_number()
   at SQLitePCL.raw.SetProvider(ISQLite3Provider imp)
   at SQLitePCL.Batteries_V2.Init()
   at SQLite.SQLiteConnection..cctor()

Repro:

SQLiteMAUITest.zip

Project

I’ve taken the standard .NET MAUI template and added SQLite to try add a local DB cache. In this example I added sqlite-net-pcl. Based on some other issues I realised I needed to install SQLitePCLRaw.bundle_green to at least v2.1.0 (which conveniently went full release just yesterday). It looks like sqlite-net-pcl has a dependency on v2.0.4 but that gave me different issues on other platforms.

All the code related to the database is in MainPage.xaml.cs. I just load the entry from the db. When user taps the the button we update the db record.

I have tested this and it appears to work on :

  • Debug+Release Android device
  • Debug on WearOS device (accidentally deployed to the wrong target)
  • Debug iOS Simulator

I am unable to deploy debug/release to my iOS device.

It does not work when I deploy as a macOS app. I am unsure if this is because it is running as a Mac Catalyst app.

I also have this nuget warning but I am unsure if it is 100% related to the main problem that is happening.

SQLitePCLRaw.core 2.1.0’ was resolved as a dependency of ‘SQLiteMAUITest’, but the dependency is using ‘Xamarin.iOS’ while ‘SQLiteMAUITest’ is using ‘net6.0-maccatalyst15.4’ as its TargetFramework. There might be compatibility issues when MacCatalyst projects depend on Xamarin.iOS projects.

Attempted Workaround

Looking into some older issues I thought maybe if I add the native library so I added SQLitePCLRaw.lib.e_sqlite3.osx but it also failed with the same error.

Additional information

I am using the latest non-beta macOS 12.4, the latest stable MAUI, the latest stable VS4Mac, latest .NET 6.0 SDK.

Detailed system information 👈
Visual Studio Community 2022 for Mac
Version 17.0 (build 8989)
Installation UUID: 4ee827da-73c9-408a-b91f-216db80a725a

Runtime
.NET 6.0.3 (64-bit)
Architecture: X64

Roslyn (Language Service)
4.1.0-3.22075.3+592501cbb9c9394072a245c15b3458ff88155d85

NuGet
Version: 6.0.0.262

.NET SDK (x64)
SDK: /usr/local/share/dotnet/sdk/6.0.300/Sdks
SDK Versions:
	6.0.300
	6.0.202
	6.0.201
	6.0.200
	6.0.105
	6.0.102
	6.0.101
	5.0.408
	5.0.407
	5.0.406
	5.0.405
	5.0.404
	5.0.403
	5.0.402
	5.0.401
	5.0.400
	5.0.302
	5.0.301
	5.0.203
	5.0.202
	5.0.201
	5.0.200
	5.0.103
	5.0.102
	5.0.101
	3.1.419
	3.1.418
	3.1.417
	3.1.416
	3.1.415
	3.1.414
	3.1.413
	3.1.412
	3.1.411
	3.1.410
	3.1.409
	3.1.408
	3.1.407
	3.1.406
	3.1.405
	3.1.404
MSBuild SDKs: /usr/local/share/dotnet/sdk/6.0.300/Sdks

.NET Runtime (x64)
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	6.0.5
	6.0.4
	6.0.3
	6.0.2
	6.0.1
	5.0.17
	5.0.16
	5.0.15
	5.0.14
	5.0.13
	5.0.12
	5.0.11
	5.0.10
	5.0.9
	5.0.8
	5.0.7
	5.0.6
	5.0.5
	5.0.4
	5.0.3
	5.0.2
	5.0.1
	3.1.25
	3.1.24
	3.1.23
	3.1.22
	3.1.21
	3.1.20
	3.1.19
	3.1.18
	3.1.17
	3.1.16
	3.1.15
	3.1.14
	3.1.13
	3.1.12
	3.1.11
	3.1.10

Xamarin.Profiler
Version: 1.8.0.19
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Updater
Version: 11

Xamarin.Android
Version: 12.3.0.3 (Visual Studio Community)
Commit: xamarin-android/d17-2/bbba5a2
Android SDK: /Users/bradmoore/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		12.0 (API level 31)
		11.0 (API level 30)
		10.0 (API level 29)

SDK Command-line Tools Version: 5.0
SDK Platform Tools Version: 31.0.3
SDK Build Tools Version: 30.0.3

Build Information: 
Mono: adf1bc4
Java.Interop: xamarin/java.interop/d17-2@9760f0a9
ProGuard: Guardsquare/proguard/v7.0.1@912d149
SQLite: xamarin/sqlite/3.38.2@7b1e016
Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-2@fc3c2ac

Microsoft Build of OpenJDK
Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk
11.0.12
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Eclipse Temurin JDK
Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk
1.8.0.302
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Android SDK Manager
Version: 17.2.0.37
Hash: 7f5a6ef
Branch: remotes/origin/d17-2
Build date: 2022-05-18 17:27:49 UTC

Android Device Manager
Version: 0.0.0.1116
Hash: e47e6c8
Branch: remotes/origin/d17-2
Build date: 2022-05-18 17:27:49 UTC

Apple Developer Tools
Xcode 13.4 (20503)
Build 13F17a

Xamarin.Mac
Version: 8.10.0.1 (Visual Studio Community)
Hash: 568bdb24e
Branch: d17-2
Build date: 2022-04-08 18:52:56-0400

Xamarin.iOS
Version: 15.10.0.1 (Visual Studio Community)
Hash: 568bdb24e
Branch: d17-2
Build date: 2022-04-08 18:52:57-0400

Xamarin Designer
Version: 17.1.5.90
Hash: 53f47efde
Branch: remotes/origin/d17-0-vsmac
Build date: 2022-05-18 17:27:43 UTC

Build Information
Release ID: 1700008989
Git revision: 55998773faa2cb1dda7aab5fecd1625f7b664d10
Build date: 2022-05-18 17:25:21+00
Build branch: release-17.0
Build lane: release-17.0

Operating System
Mac OS X 12.4.0
Darwin 21.5.0 Darwin Kernel Version 21.5.0
    Tue Apr 26 21:08:22 PDT 2022
    root:xnu-8020.121.3~4/RELEASE_X86_64 x86_64

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 15 (5 by maintainers)

Most upvoted comments

I just wanted to “confirm” that I have the same issue with VS Mac preview 17.3, where the project crashes when built and deployed by VS and works when built through the CLI.

Also, would it be possible to change the Project SDKs from “Xamarin.Legacy.Sdk” to “Microsoft.NET.Sdk”? That would just be a convenience item, as my projects (net6.0) generate several build warnings regarding the possible “incompatibility issues when MacCatalyst projects depend on Xamarin.iOS projects. (NU1703)”

Double checked and I get the same issue on the latest VS for Mac 17.3 preview.

Detailed system information 👈

Visual Studio Community 2022 for Mac Preview
Version 17.3 Preview (17.3 build 1038)
Installation UUID: 17858189-979e-4185-bbb7-408693e9f40a

Runtime
.NET 6.0.5 (64-bit)
Architecture: X64

Roslyn (Language Service)
4.3.0-2.22307.14+b91fc5b06135deb5bd72c105a1b96ce278ad6beb

NuGet
Version: 6.0.0.262

.NET SDK (x64)
SDK: /usr/local/share/dotnet/sdk/6.0.301/Sdks
SDK Versions:
	6.0.301
	3.1.420
MSBuild SDKs: /Applications/Visual Studio (Preview).app/Contents/MonoBundle/MSBuild/Current/bin/Sdks

.NET Runtime (x64)
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	6.0.6
	5.0.17
	3.1.26

Xamarin.Profiler
Version: 1.8.0.19
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Updater
Version: 11

Xamarin.Android
Version: 12.3.3.3 (Visual Studio Community)
Commit: xamarin-android/d17-2/4e061b7
Android SDK: /Volumes/Storage/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		12.0 (API level 31)
		11.0 (API level 30)

SDK Command-line Tools Version: 5.0
SDK Platform Tools Version: 31.0.3
SDK Build Tools Version: 30.0.3

Build Information: 
Mono: dffa5ab
Java.Interop: xamarin/java.interop/d17-2@9760f0a9
ProGuard: Guardsquare/proguard/v7.0.1@912d149
SQLite: xamarin/sqlite/3.38.2@7b1e016
Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-2@fc3c2ac

Microsoft Build of OpenJDK
Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk
11.0.12
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Eclipse Temurin JDK
Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk
1.8.0.302
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Android SDK Manager
Version: 17.2.0.47
Hash: d6dd135
Branch: remotes/origin/dev/tondat/main-menues~3
Build date: 2022-06-17 20:21:57 UTC

Android Device Manager
Version: 0.0.0.1127
Hash: 4bb4d48
Branch: remotes/origin/dev/tondat/main-1536295~1
Build date: 2022-06-17 20:21:57 UTC

Apple Developer Tools
Xcode 13.4.1 (20504)
Build 13F100

Xamarin.Mac
Version: 8.10.0.5 (Visual Studio Community)
Hash: 96b3edb6d
Branch: d17-2
Build date: 2022-05-18 07:32:06-0400

Xamarin.iOS
Version: 15.10.0.5 (Visual Studio Community)
Hash: 96b3edb6d
Branch: d17-2
Build date: 2022-05-18 07:32:07-0400

Xamarin Designer
Version: 17.3.0.110
Hash: 01d90a6c4
Branch: remotes/origin/d17-3
Build date: 2022-06-17 20:21:53 UTC

Build Information
Release ID: 1703001038
Git revision: ab015cef741e20e3cc212648659ecbd4ed089d5a
Build date: 2022-06-17 20:19:58+00
Build branch: release-17.3
Build lane: release-17.3

Operating System
Mac OS X 12.4.0
Darwin 21.5.0 Darwin Kernel Version 21.5.0
    Tue Apr 26 21:08:22 PDT 2022
    root:xnu-8020.121.3~4/RELEASE_X86_64 x86_64

From over in #497, @richardschoen : To confirm, that your problem is this one, I need the actual error message, which nuget packages you are using, etc.

I get the (presumably) same crash in a “classic” Xamarin.Mac app (no .NET6) - same stack trace.

System.DllNotFoundException has been thrown
e_sqlite3 assembly:<unknown assembly> type:<unknown type> member:(null)

I’ve reported this before here with a workaround that seems to work, but it didn’t get any traction.

VERY nice bug report.

I’m looking into it.