c# - 如何找出哪个nuget包导致异常?
问题描述
我正在尝试通过使用WebApplicationFactory
类和 NUnit 块在我的 .net 5.0 项目中实现单元测试。
public class CustomWebApplicationFactory<TStartup>
: WebApplicationFactory<TStartup> where TStartup: class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var descriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<AppDatabaseContext>));
services.Remove(descriptor);
services.AddDbContext<AppDatabaseContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
});
var sp = services.BuildServiceProvider();
using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<AppDatabaseContext>();
var logger = scopedServices
.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();
db.Database.EnsureCreated();
try
{
Utilities.InitializeDbForTests(db);
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred seeding the " +
"database with test messages. Error: {Message}", ex.Message);
}
}
});
}
}
我正在CustomWebApplicationFactory
我的 Setup 方法中创建一个实例:
public class BaseTests
{
protected HttpClient _client;
protected CustomWebApplicationFactory<Startup> _customWebApplicationFactory;
[OneTimeSetUp]
public void Setup()
{
_customWebApplicationFactory = new CustomWebApplicationFactory<Startup>();
_client = _customWebApplicationFactory.CreateClient();
}
}
但是当我调用我的测试时,_client.GetAsync("myURL")
我得到了这个错误:
System.MissingMethodException:找不到方法:
'System.Linq.Expressions.Expression DbSetAccessRewritingExpressionVisitor.Rewrite(Microsoft.EntityFrameworkCore.Metadata.IModel,System.Linq.Expressions.Expression)'。
我已经阅读了有关此异常的信息,并且从我发现它发生在 nuget 包不兼容/太旧时。我尝试更新所有已安装的 nuget 包,但没有帮助。如果这确实是由不兼容的 nuget 包引起的错误,我如何找出哪两个相互冲突?
解决方案
我通过卸载作为预发布版本的 NuGet 包解决了这个问题。
推荐阅读
- visual-studio-code - 允许自动缩进二维数组(如表格)的编辑器或扩展
- c++ - 将音频流式传输到 wit.ai 时 esp32 上的 HTTP 错误
- javascript - 如何将 AJAX (JS) 值传递给它?
- python - 在新的看不见的数据集中评估我的模型
- mysql - 我的 SQL 查询两个日期之间的差异?
- powershell - 使用 Robocopy 列出远程计算机中的大文件并将其保存为 ServerName.CSV
- javascript - Angular 9:forkJoin 订阅不起作用
- javascript - 如何打开模态窗口并将模型传递给js
- reactjs - 如何在 nextjs 中导航而不丢失标题组件的状态
- sass - 每次迭代具有不同动画距离的 SASS 迭代