ericsink / SQLitePCL.raw

A Portable Class Library (PCL) for low-level (raw) access to SQLite
Apache License 2.0
523 stars 109 forks source link

MissingMethod exceptions on Xamarin Targets #462

Open glennawatson opened 2 years ago

glennawatson commented 2 years ago

This seems to happen with later versions of VS2019 and VS2022

But when moving the same app with no modification on VS 2022, I get a

System.MissingMethodException: Method not found: int SQLitePCL.raw.sqlite3_bind_blob(SQLitePCL.sqlite3_stmt,int,System.ReadOnlySpan1)'

It wasn't happening before then. Only happens on certain Xamarin targets.

Here are some other links to repos where the issue is happening https://github.com/praeclarum/sqlite-net/issues/1074 https://github.com/reactiveui/Akavache/issues/735

There is a minimal repro in this closed issue which was a duplicate https://github.com/reactiveui/Akavache/issues/738

I've released a new version of Akavache with all the latest libraries and built with VS2022 but doesn't seem to help users

ericsink commented 2 years ago

When building the repro project, I see a bunch of warnings about conflicting versions of SQLitePCLRaw stuff.

It is interesting to note that the new Akavache release mentioned above seems to have resolved another issue here, #461

Overall, it seems likely that the problem here is related to multiple versions of SQLitePCLRaw in play, but (1) I am not yet certain of that, and (2) I don't yet see how the change from VS 2019 -> 2022 would trigger the problem.

glennawatson commented 2 years ago

I wonder if Microsoft is including sqliteraw as part of the IDE build now or something and that's overriding it. Will investigate that one today

ericsink commented 2 years ago

@glennawatson FWIW, I sort of doubt that path of investigation will be fruitful.

SQLitePCLRaw is indeed part of the IDE, as it is used by the Roslyn compiler. But that has been true for years, well before VS 2022 came on the scene, and I have never seen a problem where Roslyn's use of the package interfered with the assembly references for user projects.

My guess is that switching to VS 2022, which involves the .NET 6 SDK, is perhaps bringing in some aspect of the new style Xamarin workloads.

I've tested with .NET 6 and everything is fine, but I know I have issues yet to be fixed with the new Xamarin workloads, but^2 it was my understanding that these wouldn't come into play until MAUI, which was delayed. But if that last clause is not true and more Xamarin-y things changed with .NET 6 than I thought, then that is a likely source of problems.

I do plan to look into this soon, but I had meetings scheduled for today and wasn't able to make much progress yet.

Luiz-Franco commented 2 years ago

@ericsink If it helps, the problem also happens on VS 2019 version 16.11.6.

ericsink commented 2 years ago

Earlier I said:

it seems likely that the problem here is related to multiple versions of SQLitePCLRaw in play

but I think I was mistaken. The warnings I mentioned were caused by a glitch that was specific to my machine.

ericsink commented 2 years ago

OK, The missing method should be in SQLitePCLRaw.core.dll, so I decided to find that assembly in the build output and confirm that it is there.

But I haven't done Android in a while, and I couldn't find the build output in the bin directory.

So I built an APK archive and unzipped it. The file I wanted was in the assemblies directory, but it's the wrong size. And ILSpy says it's not even an assembly. So that's weird.

ericsink commented 2 years ago

I'm using the repro project mentioned above, in VS 2022. I've updated the Akavache nuget package. I'm using a default-ish Android emulator, a Pixel 5, API 30, which Visual Studio offered to setup for me yesterday.

When I run the app in Debug, I see the MissingMethod exception.

When I run it in Release, it seems to work fine, with no exception.

Anybody else seeing this?

Luiz-Franco commented 2 years ago

@ericsink As far as I was able to verify, the error only happens on debug. The release versions builded through azure pipelines did not report the error. Since then I downgraded VS from 16.11.6 to 16.10.4 to continue debugging without issues.

ericsink commented 2 years ago

FWIW, I updated Visual Studio to 17.0.2 and it did not fix the problem.

ericsink commented 2 years ago

Here's a content-free update just to say that I have not forgotten this issue but so far I am stumped.

It's possible this is a bug in Xamarin tooling. After all, things worked, and then they stopped, and it was Visual Studio that changed, not SQLitePCLRaw. If the bug is in Xamarin, it seems likely that other projects might be suffering from similar symptoms.

It's also possible that the problem is in my code. For example, perhaps I've been doing something "wrong" all along, and until recently, Visual Studio was choosing to overlook my transgression, but now it is stricter.

Geronatsios commented 2 years ago

I am getting the same error in my project (VS 2022 17.0.1) System.MissingMethodException: Method not found: int SQLitePCL.raw.sqlite3_bind_blob(SQLitePCL.sqlite3_stmt,int,System.ReadOnlySpan`1).... Any news ?

ericsink commented 2 years ago

@Geronatsios Do all of the details of your situation match the others here?

It happens on Android?

Does it happen on any other platform?

It only happens on debug builds?

Akavache is involved?

ericsink commented 2 years ago

Note (mostly to myself):

If in fact

praeclarum/sqlite-net#1074

is the same issue, then the problem is NOT specific to Android, and may not be specific to situations involving Akavache.

Right now it feels like I'm missing something fairly obvious, and I'm going to feel really stupid when I realize it.

rhinosafari commented 2 years ago

Visual Studio 2022 17.0.3 just released, and it seems to have resolved the issue for me. I noticed an issue in the release notes, Bug in Visual studio 2022 + Xamarin + SignalR (method not found), that seems to have been the culprit.

ericsink commented 2 years ago

@rhinosafari Wow, I do hope that update resolves the problem for everybody. :-)

glennawatson commented 2 years ago

https://developercommunity.visualstudio.com/t/Bug-in-Visual-studio-2022--Xamarin--Si/1528510

Geronatsios commented 2 years ago

I confirm in VS 2022 17.0.3 release it seems to have resolved the issue for me.

EmmanVazz commented 2 years ago

VS 2022 17.0.3 fixed this issue for me as well.

messiassouza commented 2 years ago

VS 2022 17.0.4 Not Working. {Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.MissingMethodException: Method not found: int SQLitePCL.raw.sqlite3_bind_blob(SQLitePCL.sqlite3_stmt,int,System.ReadOnlySpan1<byte>) at Microsoft.Data.Sqlite.SqliteValueBinder.Bind () <0x114452128 + 0x000d6> in <40b83140d09f43f4b6250353f77e03ed>:0 at Microsoft.Data.Sqlite.SqliteParameter.Bind (SQLitePCL.sqlite3_stmt stmt) <0x114442830 + 0x000b8> in <40b83140d09f43f4b6250353f77e03ed>:0 at Microsoft.Data.Sqlite.SqliteParameterCollection.Bind (SQLitePCL.sqlite3_stmt stmt) <0x114441da8 + 0x0003c> in <40b83140d09f43f4b6250353f77e03ed>:0 at Microsoft.Data.Sqlite.SqliteCommand+<GetStatements>d__54.MoveNext () <0x113fcc7e0 + 0x000a8> in <40b83140d09f43f4b6250353f77e03ed>:0 at Microsoft.Data.Sqlite.SqliteDataReader.NextResult () <0x113fcb6c0 + 0x00246> in <40b83140d09f43f4b6250353f77e03ed>:0 at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader (System.Data.CommandBehavior behavior) <0x113fc34d8 + 0x0014e> in <40b83140d09f43f4b6250353f77e03ed>:0 at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReader (System.Data.CommandBehavior behavior) <0x1125e26f0 + 0x0000a> in <40b83140d09f43f4b6250353f77e03ed>:0 at System.Data.Common.DbCommand.ExecuteReader () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.Data.Common/src/System/Data/Common/DbCommand.cs:110 at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader (Microsoft.EntityFrameworkCore.Storage.RelationalCommandParameterObject parameterObject) <0x1125d8be8 + 0x00234> in <53c13ae0c80f4b2784ab381628f2ecf7>:0 at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute (Microsoft.EntityFrameworkCore.Storage.IRelationalConnection connection) <0x11faa1f50 + 0x0008a> in <53c13ae0c80f4b2784ab381628f2ecf7>:0 --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute (Microsoft.EntityFrameworkCore.Storage.IRelationalConnection connection) <0x11faa1f50 + 0x0012c> in <53c13ae0c80f4b2784ab381628f2ecf7>:0 at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute (System.Collections.Generic.IEnumerable1[T] commandBatches, Microsoft.EntityFrameworkCore.Storage.IRelationalConnection connection) <0x11f3722f8 + 0x0020e> in <53c13ae0c80f4b2784ab381628f2ecf7>:0 at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges (System.Collections.Generic.IList1[T] entries) <0x11f37c728 + 0x0007c> in <53c13ae0c80f4b2784ab381628f2ecf7>:0 at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges (System.Collections.Generic.IList1[T] entriesToSave) <0x11f37c0d0 + 0x00040> in :0 at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges (Microsoft.EntityFrameworkCore.DbContext _, System.Boolean acceptAllChangesOnSuccess) <0x11f89f208 + 0x000de> in :0 at (wrapper delegate-invoke) System.Func3[Microsoft.EntityFrameworkCore.DbContext,System.Boolean,System.Int32].invoke_TResult_T1_T2(Microsoft.EntityFrameworkCore.DbContext,bool) at Microsoft.EntityFrameworkCore.Storage.NonRetryingExecutionStrategy.Execute[TState,TResult] (TState state, System.Func3[T1,T2,TResult] operation, System.Func`3[T1,T2,TResult] verifySucceeded) <0x11f89ea00 + 0x00028> in :0 at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges (System.Boolean acceptAllChangesOnSuccess) <0x11f89e688 + 0x00074> in :0 at Microsoft.EntityFrameworkCore.DbContext.SaveChanges (System.Boolean acceptAllChangesOnSuccess) <0x11f2dec88 + 0x0022e> in :0 at Microsoft.EntityFrameworkCore.DbContext.SaveChanges () <0x11f2de348 + 0x00008> in :0 at BP.Mob.SalesExpress.ViewModels.Sincronizacao.UiSincronizacaoViewModel+<>c__DisplayClass23_1.b__2 (BP.Mob.SalesExpress.ViewModels.Obj.ColaboradorViewModel item) [0x00509] in C:\Users\kizar\source\repos\messiassouza\BytePoucH\BP.Mob.SalesExpress\BP.Mob.SalesExpress\ViewModels\Sincronizacao\UiSincronizacaoViewModel.cs:282 }

BlueRaja commented 2 years ago

I am on VS 2022 17.0.5 and the issue is happening for me on iOS.

Disabling Hot Reload, as suggested in one of the above links, does not work :(

It also does not work in Release mode.

ericsink commented 2 years ago

@glennawatson So are you folks over in Akavache land seeing a regression? Like it was fixed in 17.0.3 and then broken again?

BlueRaja commented 2 years ago

On this page another user discovered that installing System.Memory 4.5.2 via Nuget fixes this issue. The issue appears in 4.5.3

Geronatsios commented 2 years ago

I have VS 17.1.0 and in iOS only , blobcache is null not init

Geronatsios commented 2 years ago

any news ? for problem....in VS 17.1. @ericsink

glennawatson commented 2 years ago

Haven't seen anything @ericsink

Geronatsios commented 2 years ago

Extra details :

Geronatsios commented 2 years ago

I have VS 17.1 Only in iOS devices I am getting this error : Method not found: int SQLitePCL.raw.sqlite3_bind_blob(SQLitePCL.sqlite3_stmt,int,System.ReadOnlySpan`1) when trying to insert an object in the cache...

            Akavache.Registrations.Start("AppName");  

            BlobCache.ApplicationName = "AppName";
            BlobCache.EnsureInitialized();
            //register:
            var cacheInstanceHelper = DependencyService.Get<IBlobCacheInstanceHelper>();
            Locator.CurrentMutable.Register<IBlobCacheInstanceHelper>(() => cacheInstanceHelper);

            cacheInstanceHelper.Init();

            Cache = cacheInstanceHelper.LocalMachineCache;
            Profile profile = new Profile() { name = "Bill" };
            try {

                Cache.InsertObject("myProfile", profile);
           catch(Exception ex){
           }
           public class PlatformBlobCacheInstanceHelper : IBlobCacheInstanceHelper
{
    private IFilesystemProvider _filesystemProvider;

    public PlatformBlobCacheInstanceHelper() { }

    public void Init() {
        _filesystemProvider = Locator.Current.GetService<IFilesystemProvider>();
        GetLocalMachineCache();
    }
        public IBlobCache LocalMachineCache { get; set; }

        private void GetLocalMachineCache() {

            var localCache = new Lazy<IBlobCache>(() => {
                _filesystemProvider.CreateRecursive(_filesystemProvider.GetDefaultLocalMachineCacheDirectory()).SubscribeOn(BlobCache.TaskpoolScheduler).Wait();
                return new SQLitePersistentBlobCache(Path.Combine(_filesystemProvider.GetDefaultLocalMachineCacheDirectory(), "blobs.db"), BlobCache.TaskpoolScheduler);
            });

            this.LocalMachineCache = localCache.Value;
        }
    }

Do you plan to resolve this error @ericsink ? I have already this class in iOS and Android public static class LinkerPreserve { static LinkerPreserve() { //throw new Exception(typeof(SQLitePersistentBlobCache).FullName); var persistentName = typeof(SQLitePersistentBlobCache).FullName; var encryptedName = typeof(SQLiteEncryptedBlobCache).FullName; } } public class PreserveAttribute : Attribute { }

ericsink commented 2 years ago

It is my understanding that this problem is due to a bug in Mono. The following link (also posted in a comment above) contains more information:

https://developercommunity.visualstudio.com/t/Bug-in-Visual-studio-2022--Xamarin--Si/1528510

In terms of "things that might make the bug go away", that page mentions some people having success with turning off XAML Hot Reload?

I don't currently know of any way I could even do a workaround.

Geronatsios commented 2 years ago

Some latest news for my problem :

nemekzg commented 2 years ago

Forcing your project to use System.Memory, version 4.5.2 somehow works around the problem.

kkppstudios commented 2 years ago

I'm running into this same System.MissingMethodException. I'm unable to downgrade System.Memory due to another package dependency. Any progress on this issue?

Btw, works on Android. I'm using VS 2022 17.1.4. Xamarin Forms 5.0.0.2401. Hot Restart to deploy from PC to iPhone 7.

ericsink commented 2 years ago

No progress that I know of. All indications are that this is a bug in the .NET/iOS code. It affects other projects, not just SQLitePCLRaw. To make progress, what probably needs to happen is a well-written bug report in the https://github.com/xamarin/xamarin-macios repo. AFAIK, that hasn't happened yet.

ericsink commented 2 years ago

This problem is maaaaaaybe related to xamarin/xamarin-macios#12416 ?

messiassouza commented 2 years ago

Forcing your project to use System.Memory, version 4.5.2 somehow works around the problem.

Como?

ericsink commented 2 years ago

I'm looking for updates on the status of this issue, hoping to get a handle on it. The problem has always seemed like a bug in the tooling, either dotnet or Visual Studio. Given the progress that has been made in those areas, I wonder if perhaps the problem has been fixed by Microsoft.

Who is still dealing with this issue?

Anyone noticing it "going away" on an update of the .NET tools?

kkppstudios commented 2 years ago

@ericsink I have a bunch to do to get to a point where I can test this again. I paused my efforts to get my app working for iOS due to this issue and a handful of others. I will be spending the next few days re-attempting this project, so hopefully I will let you know within a few days. Thanks for checking in! 👏

Update: 12 days later, I was not successful in migrating my Xamarin Forms app to Maui. Apparently I am not alone in the issues I am seeing. For this issue, I won't be providing an update anytime soon.