Fody / Costura

Embed references as resources
MIT License
2.4k stars 274 forks source link

Microsoft.Data.Sqlite #1004

Open wvdvegt opened 5 days ago

wvdvegt commented 5 days ago

Is there (already) a working solution/guide to include 'Microsoft.Data.Sqlite' (v8.0.1 in my case) in a project with Fody.Costura enabled (v5.0.7)?

Compiling/builing is ok but when started it does not get far into the database code and trips on path issues.

The most suggested methods, using costura folders and Unmanaged64Assemblies / Unmanaged32Assemblies xml section fails (I put for example e_sqlite3.dll into these sections and various other sugestions by google) but without (working) result.

GeertvanHorrik commented 5 days ago

I recommend trying the latest 6.0 beta since that also supports .net core runtime. Also a full FodyWeavers.xml would be helpful. Please also check the embedded libraries via ILSpy (or a similar tool).

0xced commented 5 days ago

I wrote How to package a single-file executable using the SQLite EF Core Database Provider on .NET Framework? on Stack Overflow on September 14, 2020. I have not tried with recent versions but this guide can probably get you on the right track.

Also, if you are deploying for .NET (Core) instead of .NET Framework you should probably consider Single-file deployment instead of Costura.

wvdvegt commented 4 days ago

I tried your post but without success.

It seems to work but SQLitePCLRaw.batteries_v2.dll keeps getting called and fails to load the assemblies not present. (For this behavior I need to remove SQLitePCLRaw.batteries_v2.dll from the ReferenceCopyLocalPaths remove tag). I can however create a connection to the sqlite db but am not able to do anything with it. I see all the e-sqlite3 methods being loaded nicely when executing the ModuleGetFunctionPointer code.

If i leave SQLitePCLRaw.batteries_v2.dll in the ReferenceCopyLocalPaths tag as in yout article, my application stops earlier in SQLite3Provider_dynamic_cdecl because it can't find the batteries dll.

FileNotFoundException: Could not load file or assembly 'SQLitePCLRaw.batteries_v2, Version=2.1.6.2060, Culture=neutral, PublicKeyToken=8226ea5df37bcae9' or one of its dependencies. The system cannot find the file specified.

I think it originates from the SqliteConnection code which calls/loads SQLitePCLRaw.batteries_v2:

static SqliteConnection()
{
    Type.GetType("SQLitePCL.Batteries_V2, SQLitePCLRaw.batteries_v2")
        ?.GetRuntimeMethod("Init", Type.EmptyTypes)
        ?.Invoke(null, null);
0xced commented 1 day ago

I updated my sample project at https://gist.github.com/0xced/701bcfec9c54bc0c151a365923b74e69 with different package references, using SQLitePCLRaw.provider.dynamic_cdecl instead of SQLitePCLRaw.bundle_e_sqlite3. This should solve the issue with the SQLitePCLRaw.batteries_v2 dll.

Here are the instructions on how to test it:

git clone https://gist.github.com/0xced/701bcfec9c54bc0c151a365923b74e69 sqlite-costura-net472
cd sqlite-costura-net472
dotnet build -c Release
.\bin\Release\net472\efsqlite.exe

This should print this message:

Microsoft.EntityFrameworkCore.Sqlite is working with Costura and e_sqlite3.

0xced commented 1 day ago

I just created a new repository that demonstrates how to package Microsoft.Data.Sqlite using Costura, with the latest package versions of everything involved: https://github.com/0xced/mdsqlite-costura

This is the definitive answer. 😉

GeertvanHorrik commented 1 day ago

Amazing work @0xced , thank you!

I don't want to spoil the party, but v6 will work a bit differently 👿 (but is backwards compatible 🤞 ), so maybe you might have to update it one more time in the near future 😉