定期我得到以下异常:

无法加载DLL 'SQLite.Interop.dll':无法找到指定的模块。(异常来自HRESULT: 0x8007007E)

我使用的是1.0.82.0。在VS2010, Win7 64操作系统下使用nuget安装。

一旦异常开始出现,它就会不断出现——在调试和发布中,在VS内部或外部运行应用程序。

阻止它的唯一方法就是退出并重新登录。不抛出异常并加载dll。 它可以工作几天,但之后又会坏掉。

有人见过这样的情况吗,有解决方案吗?


当前回答

Mine didn't work for unit tests either, and for some reason Michael Bromley's answer involving DeploymentItem attribute didn't work. However, I got it working using test settings. In VS2013, add a new item to your solution and search for "settings" and select the "Test Settings" template file. Name it "SqliteUnitTests" or something and open it. Select "Deployment" off to the right, and add Directory/File. Add paths/directories to your SQLite.Interop.dll file. For me, I added two paths, one each for Project\bin\Debug\x64 and Console\bin\Debug\x86. You may also want to add your Sqlite file, depending on how you expect your unit test/solution to access the file.

其他回答

当您处于这种状态时,尝试执行Rebuild-All。如果这解决了问题,您可能会遇到与我相同的问题。

一些背景(我的理解):

SQLite has 1 managed assembly (System.Data.SQLite.dll) and several platform specific assemblies (SQLite.Interop.dll). When installing SQLite with Nuget, Nuget will add the platform specific assemblies to your project (within several folders: \x86, \x64), and configures these dlls to "Copy Always". Upon load, the managed assembly will search for platform specific assemblies inside the \x86 and \x64 folders. You can see more on that here. The exception is this managed assembly attempting to find the relevant (SQLite.Interop.dll) inside these folders (and failing).

我的情况:

我的解决方案中有两个项目;一个WPF应用程序和一个类库。WPF应用程序引用类库,类库引用SQLite(通过Nuget安装)。

对我来说,问题是当我只修改WPF应用程序时,VS试图进行部分重建(意识到依赖的dll没有改变)。在这个过程的某个地方,VS清理了\x86和\x64文件夹的内容(清除了SQLite.Interop.dll)。当我做一个完整的Rebuild-All时,VS正确地复制文件夹及其内容。

我的解决方案:

为了解决这个问题,我添加了一个Post-Build进程,使用xcopy强制将\x86和\x64文件夹从类库复制到我的WPF项目\bin目录。

或者,您可以使用构建配置/输出目录做一些更花哨的事情。

我已经开始使用Costura了。Fody可以打包(.net)程序集,并嵌入和预加载本地dll。这也有助于以后的分发,因为您可以发送一个文件。

Install Costura Fody from Nuget. In your C# project create a folder called costrua32. In there add any native dlls you which C# to load. Once you have added them to this folder. Click on the properties window and change build action to "Embedded Resource" Finally you need to amend the XML file called FodyWeavers.xml as follows. Here I am specifying load the sql dll first. (note you drop the .dll) Weavers Costura PreloadOrder SQLite.Interop tbb_debug tbb /PreloadOrder> /Costura /Weavers

这样做的好处是,您不必编写任何构建前或构建后事件,最终产品完全封装在一个较大的文件中。

I got the same problem. However, finally, I can fix it. Currently, I use Visual Studio 2013 Community Edition. I just use Add->Existing Item... and browse to where the SQLite.Data.SQLite files are in (my case is 'C:\Program Files (x86)\System.Data.SQLite\2013\bin'). Please don't forget to change type of what you will include to Assembly Files (*.dll; *.pdb). Choose 'SQLite.Interop.dll' in that folder. From there and then, I can continue without any problems at all. Good luck to you all. ^_^ P.S. I create web form application. I haven't tried in window form application or others yet.

旧项目文件格式

即以<Project ToolsVersion="3.5" DefaultTargets="Build"开头的项目xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

将以下内容添加到“主”/根项目的csproj中

<PropertyGroup> 
    <ContentSQLiteInteropFiles>true</ContentSQLiteInteropFiles>
    <CopySQLiteInteropFiles>false</CopySQLiteInteropFiles>
    <CleanSQLiteInteropFiles>false</CleanSQLiteInteropFiles>
    <CollectSQLiteInteropFiles>false</CollectSQLiteInteropFiles>
</PropertyGroup>

新的SDK项目文件格式

例如,以<Project Sdk="Microsoft.NET.Sdk.*"开头的项目>

将PrivateAssets="none"添加到System.Data.Sqlite PackageImport依赖链中的每个ProjectReference/PackageImport

ex:

<PackageReference Include="System.Data.SQLite.Core" Version="1.0.110" PrivateAssets="none"/>

如果您试图在64位项目中运行32位dll,也可能会出现此错误。

当我在x86和x64文件夹中放置相同的文件(32位版本的SQLite.Interop.dll)时,我得到了这个。