首页 > 解决方案 > 加载程序集时出现异常:无法加载程序集“Microsoft.Data.Sqlite”

问题描述

我是一名长期的 ASP.NET 开发人员,但对 Xamarin 很陌生。我有一个非常简单的 Android NETStandard Xamarin 项目,它基于 Visual Studio 2017 中的 Core 2.0 构建。我刚刚开始构建我的应用程序。在公共项目中,我使用 Microsoft.Data.Sqlite 来处理本地数据库。这是除了 Xamarin.Forms 和 NETStandard 之外我引入的唯一库。

编辑:我应该注意,在我从 Android API 26 升级到 27 之前,这个应用程序过去编译得很好。我没有更改任何代码。问题不在于我的代码,而在于项目配置或 NuGet。

这是与任何特定文件无关的编译时错误:

加载程序集时出现异常:System.IO.FileNotFoundException:无法加载程序集“ Microsoft.Data.Sqlite,版本=2.1.0.0,Culture=neutral,PublicKeyToken=adb9793829ddae60”。也许它在 Mono for Android 配置文件中不存在?文件名:Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve 中的“Microsoft.Data.Sqlite.dll”(AssemblyNameReference 参考,ReaderParameters 参数)在 Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver 解析器,ICollection`1 程序集, Xamarin.Android.Tasks.ResolveAssemblies.Execute(DirectoryAssemblyResolver 解析器)上的 AssemblyDefinition 程序集,布尔顶级)

我不知道为什么 Android 应用程序需要 Sqlite,因为数据库的东西正在由 common 项目处理。这似乎是一个红鲱鱼,但我不能让这个错误消失。

这是 MyApp 的 NuGet(通用项目):

常见项目的 NuGet 屏幕截图

这是 MyApp.Android 的 NuGet:

MyApp.Android 的 NuGet 屏幕截图

我的第一直觉是这个长长的清单是不对的,但我所做的只是Install-Package Xamarin.Forms和所有其他事情都随之而来。在安装 Xamarin.Forms 之前,我删除了所有 NuGet 包。我已经和 NuGet 斗争了一段时间,所以“默认”列表不再一样了。我曾经在这里使用过 Crosslight,除其他外,可能是 Sqlite。当我创建一个全新的 Xamarin Forms 应用程序时,只有六个包:Xamrin.Forms (v3.1.0)、Xamarin.Android.Support.v4 (v27.0.2)、...v7.AppCompat (v27.0.2) , ...v7.CardView (v27.0.2) 和 ...v7.MediaRouter (v27.0.2),尽管它们不是最新版本。

我已经用尽了谷歌。我能找到的最接近解决这个问题的是 Mono 的这个 GitHub 问题,但具体的库是 System.Drawing.Common,而不是 Microsoft.Data.Sqlite。就该线程而言,这是 Mono 的一个错误。如果是这样,解决方法是什么?

我是否应该担心 MyApp.Android 混合了 Support.v4 和 Support.v7?

如果这是一个线索,MyApp 已经经历了很多按摩和使用 NuGet 来让各种事情发挥作用。我已经安装了许多库,后来又删除了它们。我已经编辑了 .sln 和 .csproj 文件,我已经多次删除了 Packages、obj 和 bin 文件夹。(.csproj 文件中未引用 Sqlite。)

我尝试将我的代码从 MyApp 复制到新的 Xamarin Forms MyApp2 ,但这有不同的问题(尽管有相同的代码,但没有找到 Sqlite 数据库并在显示 toast 通知时崩溃)。上周,当一切都在运行时以及在我尝试将 API 从 26 升级到 27 之前,MyApp 没有出现这些问题。

是的,我已经无数次地清理和重建了我的项目,并清除了 NuGet 缓存。

我正在尝试使用(最新的)Android API 27。我相信我在 API 26 下创建了 MyApp,但我不确定。

我要做的就是为使用 Microsoft.Data.Sqlite 的 Android API 27 创建一个 Xamarin Forms 应用程序!为什么这么难?我在这个项目上花费了超过 90% 的时间来对抗我没有编写的代码!

标签: visual-studiosqlitexamarin.androidmononuget

解决方案


Xamarin Forms/Visual Studio can easily get confused. Sometimes certain errors may go away just be restarting VS. I generally will close VS, nuke all obj + bin directores, and nuke the .vs directory. That will clean a lot of the cached cruff. Always close + reopen VS after change the major XF versions. (2.0<->2.5, 2.5<->3.0, 3.0<->3.1, etc.)

Beyond that, due to the large dependency tree of nuget packages, both explicitly referenced, and implicitly, it's possible to get into situation where various nuget packages won't be compatible.

Mono.Data.Sqlite is a strange case because there is some redundancy between the mono assemblies and the .net assemblies, and you have to specifically add that assembly in. See: https://forums.xamarin.com/discussion/99947/how-to-reference-mono-data-sqlite You may have to explicitly go and find that assembly and add it to your project. bleh.

Generally I don't use that for sqlite, and prefer https://github.com/praeclarum/sqlite-net and https://github.com/bordoley/SQLitePCL.pretty. I realize that other nuget packages may depend on Mono.Data.Sqlite


推荐阅读