c# - Unable to load Npgsql from a .NET class library xUnit test
问题描述
I'm learning a bit of entity framework, and have started writing some test cases around it.
I'm running into a problem whereby I am unable to load an assembly in the testcase, even though I can do so just fine (and the versions of the assemblies are the same) in the .net app itself.
The error I'm getting:
Message: System.IO.FileLoadException : Could not load file or assembly 'Npgsql.EntityFrameworkCore.PostgreSQL, Version=2.0.2.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' or one of its dependencies. Strong name signature could not be verified. The assembly may have been tampered with, or it was delay signed but not fully signed with the correct private key. (Exception from HRESULT: 0x80131045)
It works in a .net app
I began with getting a windows forms app running OK. This is using SimpleInjector, EF Core, Npgsql against a local postgres DB. I have got this to work just fine (DB creation, migrations, etc), with the DataContext (my DbContext) being loaded like so:
DbContextOptionsBuilder builder = new DbContextOptionsBuilder<DataContext>();
DbContextOptions<DataContext> options = (DbContextOptions<DataContext>)builder.UseNpgsql(connectionString).Options;
return new DataContext(options,NLog.LogManager.GetCurrentClassLogger());
I'm able to do a simple test of listing the Organizations from the DB. This is currently done in Form1.
So, onto the tests (where I'm having problems)
I added the same dependencies to a new "Testing" class library (EF Core, Npgsql, and so on). I have a dependency on the EntityFrameworkTest project as well, so I can reference its models.
I've simplified the test case (OrgDBTest) so that it attempts to create a context within it's constructor. I've done this so that I know my problem has nothing to do with SimpleInjector.
My expectation is that the test would run, creating a DbContext in its constructor. However I can't get it to load the assembly so that I can create my DbContext... so that I can continue creating/playing with tests.
Code is here (pretty small): https://github.com/scornflake/entityframework-learn
But lost, as it seems I'm doing the same thing in the testcase as I am in the app, yet assembly loading appears to be somehow different.
Any ideas?
解决方案
I don't have the underlying reason, but switching from xUnit to NUnit makes everything work. I can only presume there's some underlying difference with the xUnit runner with regard to assembly loading / security (I wish I knew what it was, but I've already spent many hours getting to this point!)
推荐阅读
- kubernetes - 在 pod 定义中使用图像哈希而不是标签
- python - 当地图在pygame中移动时用鼠标找到一个项目
- python - 如何在 python openshift rest 客户端中使用 load_incluster_config 时修复“没有这样的文件或目录:'/home/jenkins/.kube/config'”
- opencv - 在 opencv 4 中使用 IplImage(和 C API)
- react-native - 从 app.js 反应本机更新 redux 商店不起作用
- c - 如何在二进制文件上写入 CSV 字段?
- python - 使用 python 覆盖测试忽略不推荐使用的方法
- elasticsearch - 运行爬虫时收到致命错误
- python - 乒乓球弹跳
- javascript - 如何禁用/启用页面滚动