简体   繁体   English

Visual Studio C# - 找不到 SQLite.Interop.dll

[英]Visual Studio C# - SQLite.Interop.dll not found

I am currently trying to create with Visual Studio a C# application working with SQLite.我目前正在尝试使用 Visual Studio 创建一个使用 SQLite 的 C# 应用程序。 I installed SQLite for my program with NuGet and three references appeared in the Solution Explorer (System.Data.SQLite, System.Data.SQLite.EF6, System.Data.SQLite.Linq).我使用 NuGet 为我的程序安装了 SQLite,解决方案资源管理器中出现了三个引用(System.Data.SQLite、System.Data.SQLite.EF6、System.Data.SQLite.Linq)。

When I execute my app in VS, everything works fine.当我在 VS 中执行我的应用程序时,一切正常。 However, whenever I publish it (with ClickOnce) and try to run the app, it crashes with the following error : System.DllNotFoundException : Unable to load DLL 'SQLite.Interop.dll' : the specified module cannot be found (Exception from HRESULT : 0x8007007E)但是,每当我发布它(使用 ClickOnce)并尝试运行该应用程序时,它都会崩溃并出现以下错误: System.DllNotFoundException : Unable to load DLL 'SQLite.Interop.dll' : the specified module can not be found (Exception from HRESULT : 0x8007007E)

After some research, I found this dll in the Debug folder and copied it in the folder where the ".exe" version of my program is created (after publishing and executing "setup.exe").经过一番研究,我在Debug文件夹中找到了这个dll,并将其复制到创建我的程序“.exe”版本的文件夹中(发布并执行“setup.exe”之后)。 This way, the app runs well.这样,应用程序运行良好。

But I'm not really satisfied with this solution, since it seems really dirty.但我对这个解决方案并不满意,因为它看起来很脏。 I don't want future users to have to do this manually when they install my app !我不希望未来的用户在安装我的应用程序时必须手动执行此操作!

Therefore, my question is : What can I do to make sure that this dll is installed together with my program ?因此,我的问题是:我能做些什么来确保这个 dll 与我的程序一起安装?

Note : I tried the first answer of this post : unable to load dll sqlite interop dll WPF But I didn't seem to work for me.注意:我尝试了这篇文章的第一个答案: 无法加载 dll sqlite interop dll WPF但我似乎没有为我工作。

I also encountered the similar issue in my environment of Visual Studio 2017 and NET Framework 4.5, the circumstance is:我在Visual Studio 2017和NET Framework 4.5的环境中也遇到过类似的问题,情况是:

I am developing an Outlook Add Ins using ClickOnce to publish it and SQLite as the database.我正在开发一个 Outlook 加载项,使用 ClickOnce 发布它和 SQLite 作为数据库。 After having installed the SQLite library into the project from NuGet, it works perfectly in the local environment but pops up the exception error message "Unable to load DLL 'SQLite.Interop.dll'" on the client machine.从NuGet 将SQLite 库安装到项目中后,在本地环境下运行正常,但在客户端机器上弹出异常错误信息“Unable to load DLL 'SQLite.Interop.dll'”。

I compared the two environments and found the DLL file 'SQLite.Interop.dll' had not been published to the client machine that we can see in the following graphic.我比较了两种环境,发现 DLL 文件“SQLite.Interop.dll”尚未发布到客户端计算机,如下图所示。 在此处输入图片说明

I searched 'SQLite.Interop.dll' in local environment and found it is saved in the following folder "{project}\\packages\\System.Data.SQLite.Core.1.0.105.2\\build\\" for the specific .Net FrameWork我在本地环境中搜索“SQLite.Interop.dll”,发现它保存在以下文件夹“{project}\\packages\\System.Data.SQLite.Core.1.0.105.2\\build\\”中,用于特定的.Net FrameWork 在此处输入图片说明

So, my solution is adding these two DLL libraries into the project, then they can be published to the client machine using the ClickOnce.因此,我的解决方案是将这两个 DLL 库添加到项目中,然后可以使用 ClickOnce 将它们发布到客户端计算机。 在此处输入图片说明

(Notice: The selected DLL version should be as same as the NET Framework version in your environment.) (注意:所选的 DLL 版本应与您环境中的 NET Framework 版本相同。)

See Using Native Library Pre-Loading at https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki请参阅https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 上的使用本机库预加载

You likely need to include the x86 and x64 folders under the the other SQLite DLL.您可能需要在另一个 SQLite DLL 下包含 x86 和 x64 文件夹。

Edit: I've added the relevant info below in case the above link ever dies/changes.编辑:我在下面添加了相关信息,以防上面的链接失效/更改。

If the development and customer machines may have different processor architectures, more than one binary package may be required.如果开发和客户机器可能具有不同的处理器架构,则可能需要多个二进制包。 For this situation, using the native library pre-loading feature is highly recommended.对于这种情况,强烈建议使用本机库预加载功能。 It is available as of version 1.0.80.0 and enabled by default.它从 1.0.80.0 版开始可用并默认启用。 In order to take advantage of this feature, the separate managed and interop assemblies must be used with XCOPY deployment (ie this feature is not supported by the mixed-mode assembly, nor when the assembly is deployed to the global assembly cache), resulting in an application deployment that looks something like this:为了利用此功能,必须将单独的托管程序集和互操作程序集与 XCOPY 部署一起使用(即混合模式程序集不支持此功能,也不在将程序集部署到全局程序集缓存时),导致一个看起来像这样的应用程序部署:

  • bin\\App.exe (optional, managed-only application executable assembly) bin\\App.exe(可选,仅限托管的应用程序可执行程序集)
  • bin\\App.dll (optional, managed-only application library assembly) bin\\App.dll(可选,仅限托管的应用程序库程序集)
  • bin\\System.Data.SQLite.dll (required, managed-only core assembly) bin\\System.Data.SQLite.dll(必需的,仅限托管的核心程序集)
  • bin\\System.Data.SQLite.Linq.dll (optional, managed-only LINQ assembly) bin\\System.Data.SQLite.Linq.dll(可选,仅限托管的 LINQ 程序集)
  • bin\\System.Data.SQLite.EF6.dll (optional, managed-only EF6 assembly) bin\\System.Data.SQLite.EF6.dll(可选,仅限托管的 EF6 程序集)
  • bin\\x86\\SQLite.Interop.dll (required, x86 native interop assembly) bin\\x86\\SQLite.Interop.dll(必需,x86 本机互操作程序集)
  • bin\\x64\\SQLite.Interop.dll (required, x64 native interop assembly) bin\\x64\\SQLite.Interop.dll(必需,x64 本机互操作程序集)

The string "bin" above represents the directory where the application binaries are to be deployed on the target machine.上面的字符串“bin”表示应用程序二进制文件将在目标机器上部署的目录。 With the native library pre-loading feature enabled and the application deployment shown above, the System.Data.SQLite managed-only assembly will attempt to automatically detect the processor architecture of the current process and pre-load the appropriate native library.启用本机库预加载功能和上面显示的应用程序部署后,System.Data.SQLite 仅托管程序集将尝试自动检测当前进程的处理器体系结构并预加载适当的本机库。

By default the dll sqlite.interop.dll does not come with NuGet installation of SQLite.默认情况下,SQLite 的 NuGet 安装不附带 dll sqlite.interop.dll。 To solve this you need to add both the dlls, x86 and x64.要解决此问题,您需要同时添加 x86 和 x64 dll。 At the SQLite website download the pack containing necessary dlls.在 SQLite 网站上下载包含必要 dll 的包。 Install them in client or copy these dlls to your Project before distributing.在分发之前将它们安装在客户端或将这些 dll 复制到您的项目中。

Please use the answer to the duplicate issue: https://stackoverflow.com/a/60176344/3634867请使用重复问题的答案: https : //stackoverflow.com/a/60176344/3634867

Don't store the dll yourself and copy it to /bin manually, it will lost control to the version updating and dependency.不要自己存储dll并手动复制到/bin,它会失去对版本更新和依赖的控制。

TL;DR; TL; 博士;

for old csproj file:对于旧的 csproj 文件:

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

for new csporj file: (ie projects beginning with <Project Sdk="Microsoft.NET.Sdk.*"> )对于新的 csporj 文件:(即以<Project Sdk="Microsoft.NET.Sdk.*">开头的<Project Sdk="Microsoft.NET.Sdk.*">

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

If it is an installable file.如果是可安装文件。 ie, if u are creating setup file, then you have to add the dll manually to the primary output file.即,如果您正在创建安装文件,那么您必须手动将 dll 添加到主输出文件中。

Go to properties of the setup project and in configuration settings, select pre-requisites SQLLite, select(Checkbox) install it from the project location.转到安装项目的属性并在配置设置中,选择先决条件 SQLLite,选择(复选框)从项目位置安装它。

See if this solves your issue.看看这是否能解决您的问题。

"Note : I tried the first answer of this post : unable to load dll sqlite interop dll WPF But I didn't seem to work for me." “注意:我尝试了这篇文章的第一个答案:无法加载 dll sqlite interop dll WPF 但我似乎不适合我。”

I tried that, too, and finally got it working.我也试过了,终于成功了。 However, important is:然而,重要的是:

  1. Insert the sqlite.interop.dll as an existing item to the root of the project you are going to build.将 sqlite.interop.dll 作为现有项目插入到您要构建的项目的根目录中。
  2. In the properties of that file, make sure its build processing is 'content' (not embedded resource as suggested) and, of course, copied to the output directory always.在该文件的属性中,确保其构建处理是“内容”(不是建议的嵌入资源),当然,始终复制到输出目录。

Hope that helps to beat that annoying bug.希望这有助于克服那个烦人的错误。

The System.Data.SQLite nuget package includes a Build folder with a targets file that will copy these interop dlls. System.Data.SQLite nuget 包包含一个 Build 文件夹,其中包含将复制这些互操作 dll 的目标文件。 To solve this, we had to tell the project we were packaging into our own nuget, to NOT exclude this build target/folder.为了解决这个问题,我们必须告诉我们要打包到我们自己的 nuget 中的项目,不要排除这个构建目标/文件夹。

Our project (the one we package and then use elsewhere) needed to have PrivateAssets="None" or PrivateAssets="Analyzers" if you want to make sure not to include Analyzers.如果您想确保不包含分析器,我们的项目(我们打包然后在其他地方使用的项目)需要有 PrivateAssets="None" 或 PrivateAssets="Analyzers"。

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    <RootNamespace>MyNamespace</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="ServiceStack.OrmLite.Sqlite.Core" Version="5.6.0" />
    <PackageReference Include="System.Data.SQLite.Core" Version="1.0.111"  PrivateAssets="Analyzers" />
   </ItemGroup>

</Project>

SQlite.Interop is not referenceable, you have to: SQlite.Interop 不可引用,您必须:

  1. In your VS project Add Existing File: Sqlite.Interop.dll actual location is .\\bin\\Debug\\x86 or \\bin\\Debug\\x64在你的 VS 项目中添加现有文件:Sqlite.Interop.dll 实际位置是 .\\bin\\Debug\\x86 或 \\bin\\Debug\\x64

  2. Select properties and set Compilation Action in "Content" and copy to the output directory = "Always Copy"选择属性并在“内容”中设置编译操作并复制到输出目录=“始终复制”

  3. Rebuild重建

将 System.Data.SQLite.Core Nuget 包升级到版本 1.0.109.2 为我修复了错误。

One alternative solution might be to replace the xcopy post build by using the System.Data.SQLite.Core.targets script file from the System.Data.SQLite.Core package for doing that.一种替代解决方案可能是通过使用System.Data.SQLite.Core包中的System.Data.SQLite.Core.targets脚本文件来替换xcopy 后构建 Here my working csproj file:这是我的工作csproj文件: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM