Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Data.Sqlite #1004

Open
wvdvegt opened this issue Nov 11, 2024 · 11 comments
Open

Microsoft.Data.Sqlite #1004

wvdvegt opened this issue Nov 11, 2024 · 11 comments

Comments

@wvdvegt
Copy link

wvdvegt commented Nov 11, 2024

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
Copy link
Member

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
Copy link
Contributor

0xced commented Nov 11, 2024

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
Copy link
Author

wvdvegt commented Nov 12, 2024

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
Copy link
Contributor

0xced commented Nov 15, 2024

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
Copy link
Contributor

0xced commented Nov 15, 2024

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
Copy link
Member

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 😉

@wvdvegt
Copy link
Author

wvdvegt commented Nov 21, 2024

Having some additional issues with the code posted (it seems to work but my code uses: Assembly.GetExecutingAssembly().Location
which returns null on executables processed by Costura.

I also have some issues debugging it (when debugging Costura does not seem to pack on running code).

@wvdvegt
Copy link
Author

wvdvegt commented Nov 21, 2024

Found the issue was partly at my side, altough while: Assembly.GetExecutingAssembly().Location

was null, it turned out it wasn't pointing to the main exe (as i thought) but to a linked projects output where a resource from this dll needed to be extracted.

Excluding the assembly from Costura and using typeof().Assembly seems much more appropriate.

@wvdvegt
Copy link
Author

wvdvegt commented Nov 21, 2024

When processing with Costura, typeof(Authenticated).Assembly.Location is also empty for the assembly i need for resource extraction (so not usable).

Seems the best option for now is to exclude the assembly from Costura treatment.

@wvdvegt
Copy link
Author

wvdvegt commented Nov 23, 2024

Everyting is working nicely (except that i get a InvalidOperationException exception when creating the SqliteConnection (but my SqliteConnection property is assigned so a try/except around it works).

Exception thrown: 'System.InvalidOperationException' in Microsoft.Data.Sqlite
The process has no package identity. (Exception from HRESULT: 0x80073D54)

Exception thrown: 'System.Reflection.TargetInvocationException' in Microsoft.Data.Sqlite

@Joe-Fernico
Copy link

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. 😉

Thank you for this! Was having trouble. Just to highlight for others trying to merge into their projects, the important stuff (the actual copying of the dlls) is the in .csproj file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants