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

Unable to load e_sqlite3.dll for Microsoft.EntityFrameworkCore.Sqlite 3.0.0 #84

Closed
Nukepayload2 opened this issue Oct 16, 2019 · 16 comments
Labels
external The cause of this problem is outside of the control of this project

Comments

@Nukepayload2
Copy link

Describe the bug
DllNotFoundException thrown when using a plugin that consumes Microsoft.EntityFrameworkCore.Sqlite 3.0.0 . If I downgrade EFCore.Sqlite to 2.2.6, the plugin works fine.

System.TypeInitializationException: 'The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception.'
DllNotFoundException: Unable to load DLL 'e_sqlite3' or one of its dependencies: The specified module could not be found. (0x8007007E)

To Reproduce
Steps to reproduce the behavior:
Version: 0.3.1
Code (In the main program):

public Assembly Load(string assemblyName)
{
    return PluginLoader.CreateFromAssemblyFile(assemblyName).LoadDefaultAssembly();
}

The plugin connects to a Sqlite3 database with Microsoft.EntityFrameworkCore.Sqlite 3.0.0 and print data to Console. (See the screenshot for details)

Expected behavior
The plugin should not throw DllNotFoundException.

Screenshots
image

Additional context

If I copy "e_sqlite3.dll" from "plugins\runtimes\win-x64\native\e_sqlite3.dll" to the "plugins" directory, the plugin won't throw DllNotFoundException.

@natemcmaster
Copy link
Owner

Two questions:

  1. Can you share the .csproj file of your plugin?
  2. How are you getting files into the plugin directory? i.e. do you run dotnet publish --output $path or something else?

@natemcmaster natemcmaster added the more info needed Please provide more information. label Oct 20, 2019
@Nukepayload2
Copy link
Author

@natemcmaster

  1. I can't share all contents of the project file because it contains sensitive information.
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="3.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />
    <PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <None Update="northwind.sqlite3">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  
</Project>
  1. dotnet publish "ProjectFileName" -c Release and then xcopy them into the "plugins" directory

@no-response no-response bot removed the more info needed Please provide more information. label Oct 22, 2019
@natemcmaster natemcmaster added the external The cause of this problem is outside of the control of this project label Oct 26, 2019
@natemcmaster
Copy link
Owner

Sorry, but I don't think there is anything I can do to fix this error. Under the hood, EF Core's SQLite provider uses https://github.com/ericsink/SQLitePCL.raw. The way SQLitePCLRaw.nativelibrary is attempting to load the "e_sqlite3.dll" library circumvents this essential callback in System.Runtime.Loader.AssemblyLoadContext.

The root cause appears to be the use of System.Runtime.InteropServices.NativeLibrary. This doesn't account for the current AssemblyLoadContext or the runtimes/$rid/native/*.dll layout. System.Runtime.Loader.AssemblyDependencyResolver might be a better library for SQLitePCLRaw to use, but I'll leave that up to @ericsink.

Workaround

Change your plugin deployment to include a runtime identifier. dotnet publish --runtime win-x64 should do the trick.

@ericsink
Copy link

Hmmm. This is interesting. Thanks for tagging me in. Not sure yet what to do about it.

@Andale-online
Copy link

Andale-online commented Dec 7, 2019

Your workaround isn't a workaround... The native sqlite library needs to know which architecture to use. At least 32/64bit... Run into this with shared libraries for xamarin - It's normal, not a bug.

If you need further help, tell me @ericsink

@natemcmaster
Copy link
Owner

You're right. Depending on your scenario, publishing the plugin using --runtime <RID> may not be feasible. Any other suggestions on how to workaround this?

By the way @ericsink, it seems like we've run into the "native" version of this "managed" problem: https://github.com/dotnet/coreclr/blob/v3.0.0/Documentation/design-docs/AssemblyLoadContext.ContextualReflection.md#problem. Basically, the API used by SQLite isn't aware of the context it is called from. I think we should request a feature in coreclr to make System.Runtime.InteropServices.NativeLibrary honor the current assembly loading context.

@ericsink
Copy link

ericsink commented Dec 9, 2019

Hmmm. That's good info. I'm getting a better understanding of the problem here.

@aderderian
Copy link

Any updates on this? Dead in the water on it currently.

@ericsink
Copy link

No significant update yet, but let's link this issue too: ericsink/SQLitePCL.raw#343

@natemcmaster
Copy link
Owner

I believe this will be addressed in .NET 5 - see dotnet/runtime#13819.

I'm closing as there is no action item here for this library. The problem is external to my code and something I can't fix in older versions of .NET.

@ravigupta0510
Copy link

Hi guys,

I was also facing the same issue. I found the solution and it's working perfectly for me. If you ask I can write a detail note on the solution.

@emilbm
Copy link

emilbm commented Aug 20, 2020

Hi guys,

I was also facing the same issue. I found the solution and it's working perfectly for me. If you ask I can write a detail note on the solution.

Yes please....

@ravigupta0510
Copy link

I found its solution and it worked for me.

Solution:

First, I Removed all possible SQLite NuGet packages and then installed only one library(Nuget package) i.e "sqlite-net-pcl"( There are a number of different SQLite packages available – be sure to choose the correct one, it might not be the top result in search.) in all relevant project. It will download all its dependent packages. Please see below screenshot:

sqlite-net-pcl

Background:

There are two visual studio solutions; one is for Android and the other one is for iOS, and there are three projects in every solution. Two projects(used as libraries) are common for android and iOS solutions. In one common project, There is some SQLite code to handle database operations. So in this project, I installed the "sqlite-net-pcl" Nuget Package to fulfill the compile-time dependency.

Root cause analysis:

When I run the app, I found there is run time exception(Library e_sqlite3 not found. I concluded that the SQLite library is needed at run time in the Android project. So I added the same "sqlite-net-pcl" Nuget Package in the Android project as well. This package downloads all its relevant dependent packages. And this worked perfectly for me.

I hope it should work for everyone.

Thank you for your patience to read the entire solution.

@AndreeaFGrosu
Copy link

Hi!
@jeremybytes have a blog post about this error here.
Maybe his solution will ever help someone, although it's a "work-around".

@ptgullas
Copy link

Hi! @jeremybytes have a blog post about this error here. Maybe his solution will ever help someone, although it's a "work-around".

Yes, his solution of manually copying the e_sqlite3.dll worked for me (I don't like it either)

@arikanhasan
Copy link

I had a windows form project running in .Net 6.0.

When I upgraded it directly to .Net 8.0.8, there was no problem working in debug, but when I published the application and installed it through setup, I encountered this error.

After trying many solutions, I finally changed the .Net version to 7.0 and the problem was solved.

Then when I downgraded to .Net 8.0.8 again, I didn't get the same error again.

I guess upgrading suddenly causes some deficiencies in such version transitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external The cause of this problem is outside of the control of this project
Projects
None yet
Development

No branches or pull requests

10 participants