SQLiteConnection constructor in System.Data.SQLite throws exception in single file bundle (.NET 6/7) without workaround
(1) By Tobias (ttobias) on 2023-07-16 17:47:55 [link] [source]
Hi,
If I bundle my application as a single file the constructor of System.Data.SQLite.SQLiteConnection(string connectionString) throws an exception and does not work without a workaround. Single file bundling combines all the CLR code into a single executable. (You still need the SQLite.Interop.dll as a separate file)
https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview?tabs=cli
The stack trace shows that it is a System.ArgumentNullException inside Path.Combine() called from the constructor which points to
ConfigureViaOneFile(Path.Combine(GetDirectory(DirectoryType.Configure), "sds-see.eagle"));
Based of this warning from Microsoft: https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/warnings/il3000 which talks about
Assembly.Location
It will return an empty string if the code is running from a single file bundle.
It looks like code like e.g. in UnsafeNativeMethods must be adapted for this new behavior.
private static string GetAssemblyDirectory()
{
try
{
Assembly executingAssembly = Assembly.GetExecutingAssembly();
The workaround I found is to set "SQLite_ConfigureDirectory" before the SQLiteConnection constructor is invoked:
Environment.SetEnvironmentVariable("SQLite_ConfigureDirectory", AppContext.BaseDirectory);
var connection = new SQLiteConnection(connectionString);
But I'm not sure if this covers all code paths. At least for me it seems to work for my application and usage scenarios.
Hopefully this can be fixed so it works out of the box in the future.
Thanks
(2) By Deni (Deni42) on 2023-07-17 06:52:10 in reply to 1 [link] [source]
I just came here to report this. Seems to be a bug in 1.0.118.0, as it was not present in the previous version. Thanks for the workaround, it also works for me.
(3.1) By mistachkin on 2023-07-18 16:33:39 edited from 3.0 in reply to 1 [source]
A workaround for this has been added to the project, so this issue should not be seen with 1.0.119.0 and later. Meanwhile, another interim workaround is available. Setting the following environment variable will disable the code that is raising the exception: System.Environment.SetEnvironmentVariable( "SQLite_NoConfigure", "1"); I would also like to note that "single file bundles" were never officially supported, primarily because we have never been able to properly test them.
(4) By Jochen Kühner (jogibear9988) on 2024-01-30 06:12:09 in reply to 3.1 [link] [source]
The issue not only occures in sigle file bundles. I've implemented my own Assembly loader, wich loads all assemblies from a ByteArray, so the code also does not work