定期我得到以下异常:

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

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

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

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

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


当前回答

我在一个WPF项目中使用SQLite时遇到了同样的问题,该项目的平台目标是任意CPU。我通过以下步骤修复了它:

在Visual Studio中打开项目设计器。如何做到这一点的细节可以在这里找到。 单击Build选项卡。 禁用首选32位选项。

或者,您也可以将平台目标设置为x86或x64。我认为这个问题是由System.Data.SQLite库使用平台目标来获取'SQLite.Interop.dll'文件的位置引起的。

更新:

如果无法联系到项目设计人员,只需从文本编辑器中打开项目(*.csproj)文件,并将值<Prefer32Bit>false</Prefer32Bit>添加到<PropertyGroup>…< / PropertyGroup >标记。

示例代码

<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>[Set by Visual Studio]</ProjectGuid>
    <OutputType>Exe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>[Set by Visual Studio]</RootNamespace>
    <AssemblyName>[Set by Visual Studio]</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>[Set by Visual Studio]</FileAlignment>
    <!--Add the line below to your project file. Leave everything else untouched-->
    <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>

其他回答

我的应用程序是一个web应用程序(ASP。NET MVC),我不得不改变应用程序池下运行的LocalSystem而不是ApplicationPoolIdentity。这样做:

打开IIS管理器 找到站点正在运行的应用程序池。 从操作中单击高级设置 将Identity更改为LocalSystem

我不知道为什么这会解决这个问题。

更新NuGet从工具->扩展和更新和重新安装SQLite。使用命令PM> Update-Package -重装System.Data.SQLite.Core为我修复了它。

我已经开始使用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

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

我不知道这是否是一个好的答案,但我能够通过在AppDomain下以“本地系统”的身份运行我的应用程序来解决这个问题。

当您处于这种状态时,尝试执行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目录。

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