Unable to load DLL 'SQLite.Interop.dll'

2019-01-02 17:26发布

Periodically I am getting the following exception:

Unable to load DLL 'SQLite.Interop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

I am using 1.0.82.0. version, installing it with nuget in VS2010, OS Win7 64.

Once exception starts to appear, it appears constantly - in debug and release and running application within or outside VS.

The only way to stop it is logoff and logon. The exception is not thrown and dll is loaded. It can work for days, but then it can break again.

Has anyone seen something like this and is there a solution for it?

30条回答
何处买醉
2楼-- · 2019-01-02 18:09

I had the same issue running Visual Studio Express 2013. I tried several solutions mentioned here and elsewhere to no avail. I hope this fix helps others.

I fixed it by using the DeploymentItem attribute on my test class that tests the SQLite-based service.

Example:

[TestClass]
[DeploymentItem(@"x86\SQLite.Interop.dll", "x86")] // this is the key
public class LocalStoreServiceTests
{

    [TestMethod]
    public void SomeTestThatWasFailing_DueToThisVeryIssue()
    {
         // ... test code here
    }
}

This causes the needed SQLite.Interop.dll to get copied to the x86 directory within the appropriate "TestResults" folder.

All is green. All is good.

查看更多
忆尘夕之涩
3楼-- · 2019-01-02 18:09

The default installation of the multi-architecture (x86, x64) version of SQLite from NuGet exhibits the behavior that you described. If you would like to load the correct version for actual architecture that the .NET runtime chose to run your application on your machine, then you can give the DLL loader a hint about where to locate the correct library as follows:

Add a declaration for the kernel32.dll function call to SetDLLDirectory() before your Program.Main():

    [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
    [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
    static extern bool SetDllDirectory(string lpPathName);

Then use your own method for determining the correct subdirectory to find the architecture specific version of 'SQLite.Interop.dll'. I use the following code:

    [STAThread]
    static void Main()
    {
        int wsize = IntPtr.Size;
        string libdir = (wsize == 4)?"x86":"x64";
        string appPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
        SetDllDirectory(System.IO.Path.Combine(appPath, libdir));
查看更多
梦醉为红颜
4楼-- · 2019-01-02 18:10

Try to set the platform target to x86 or x64 (and not Any CPU) before you build: Project->Properties->Build->Platform target in Visual Studio.

查看更多
素衣白纱
5楼-- · 2019-01-02 18:10

Updating NuGet from Tools -> Extension and updates and reinstalling SQLite.Core with the command PM> Update-Package -reinstall System.Data.SQLite.Core fixed it for me.

查看更多
大哥的爱人
6楼-- · 2019-01-02 18:11

I've struggled with this for a long time, and, occasionally, I found that the test setting is incorrect. See this image: Test setting

I just uncheck the test setting, and the issue disappears. Otherwise, the exception will occurs. Hopefully, this will help someone. Not sure it's the root cause.

查看更多
与风俱净
7楼-- · 2019-01-02 18:12

As the SQLite wiki says, your application deployment must be:

Application deployment

So you need to follow the rules. Find dll that matches your target platform and put it in location, describes in the picture. Dlls can be found in YourSolution/packages/System.Data.SQLite.Core.%version%/.

I had problems with application deployment, so I just added right SQLite.Interop.dll into my project, the added x86 folder to AppplicationFolder in setup project and added file references to dll.

查看更多
登录 后发表回答