【问题标题】:Using SQLiteAsyncConnection throwing a MissingMethodException使用 SQLiteAsyncConnection 抛出 MissingMethodException
【发布时间】:2016-06-10 16:35:48
【问题描述】:

第一次使用SQLite,选择了SQLite.net-pcl

我有一个PCL 库,我从我的UWP 应用程序中使用它,我的PCL 库有DBContext 类:

public NewspaperDataContext(ISQLitePlatform sqLitePlatform, string applicationPath, string dataBaseName)
{
     _sqlConnection = new SQLiteAsyncConnection(()=> new SQLiteConnectionWithLock(sqLitePlatform, 
     new SQLite.Net.SQLiteConnectionString(Path.Combine(applicationPath, dataBaseName), storeDateTimeAsTicks: false)));

     CreateTableAsync(_sqlConnection, typeof(Article)).Result.Wait();
     CreateTableAsync(_sqlConnection, typeof(Author)).Result.Wait();
     CreateTableAsync(_sqlConnection, typeof(Category)).Result.Wait();
     CreateTableAsync(_sqlConnection, typeof(Group)).Result.Wait();

     var c = _sqlConnection.Table<Article>();
}

private async Task<Task> CreateTableAsync(SQLiteAsyncConnection asyncConnection, Type table)
{
     return asyncConnection.CreateTableAsync<Author>().ContinueWith((results) =>
     {
           Debug.WriteLine(!results.IsFaulted
           ? $"Error where creating the {nameof(table)} table !!"
           : $"Table {nameof(table)} created sucessfully!");
     });
}

我从我的 UWP 应用程序中调用 NewspaperDataContext,如下所示:

private async void MainPage_OnLoaded(object sender, RoutedEventArgs e)
{            
    var n = new NewspaperDataContext(new SQLitePlatformWinRT(), ApplicationData.Current.LocalFolder.Path, "LiberteDB");
}

在 NewspaperDataContext 构造函数中的一行:

var c = _sqlConnection.Table<Article>();

抛出一个奇怪的 MissingMethodException 说:

“System.MissingMethodException”类型的异常发生在 SQLite.Net.Async.dll 但未在用户代码中处理

附加信息:找不到方法:'无效 SQLite.Net.SQLiteConnectionString..ctor(System.String,布尔值, SQLite.Net.IBlobSerializer, SQLite.Net.IContractResolver)'。

在互联网上找不到与此错误相关的任何内容,请有人帮忙。

【问题讨论】:

  • 当尝试动态访问不存在的方法时会抛出此异常。我认为问题可能在于您创建了一个必须具有返回类型的方法NewspaperDataContext,但您的代码中没有返回值。你调用这个方法就像创建一个名为NewspaperDataContext的类的新实例。如果这个方法是 void 类的,你能不能试着用这个代码NewspaperDataContext(new SQLitePlatformWinRT(), ApplicationData.Current.LocalFolder.Path, "LiberteDB");调用这个方法?
  • 或者,如果有返回值,请您尝试像var n = NewspaperDataContext(new SQLitePlatformWinRT(), ApplicationData.Current.LocalFolder.Path, "LiberteDB");一样调用这个方法吗?抱歉,如果我误解了您的 NewspaperDataContext 方法。但我认为问题可能与您如何调用此方法有关。
  • 您了解这个问题的根源了吗?我也有类似的问题
  • 发现是重新添加了导致我的问题的旧包。 github.com/oysteinkrog/SQLite.Net-PCL/issues/14

标签: c# sqlite win-universal-app portable-class-library sqlite-net


【解决方案1】:

sqlite-net-pcl 应该只添加到PCL 项目中。如果你将它添加到任何其他平台,例如 Android,它会给出这个MethodMissingException。因此,请从其他平台删除该包,因为此包仅适用于 PCL 项目。我给出了这个答案,因为我遇到了同样的问题并找到了解决方案。我在使用 Xamarin.Forms 开发应用程序时遇到了这个异常。

【讨论】:

  • 来自here:“重要提示:您需要将 NuGet 包添加到您的 .NET Standard 库项目和依赖于平台的应用程序项目中。” - 所以可能取决于版本?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
相关资源
最近更新 更多