Unable to load Npgsql from a .NET class library xUnit test

1

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?

c#
entity-framework
xunit
npgsql
asked on Stack Overflow May 2, 2018 by scornflake

1 Answer

0

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!)

answered on Stack Overflow May 5, 2018 by scornflake

User contributions licensed under CC BY-SA 3.0