Background
I had a VS 2022 solution utilising .NET Core 3. I wanted to upgrade it to .NET Core 9 so I “upgraded” the projects withing the solution and updated the nugets to reflect later versions utilising “9”. So far so good
I ran up the WPF UI – this worked correctly, entities were read from SQL via entity framework and all was good and sunny.
Then I ran my unit tests. All good
Then I ran my integration tests. They failed! But, to be fair, they are fairly noddy tests but at least they tested reading/writing entities to the database to test the sanity and functionality of my code. So I had a problem
The Problem
The error returned from the tests (utilising XUnit) was
System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
/// Could not load type 'SqlGuidCaster' from assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0,
Hmm. I checked my solution. I don’t reference Microsoft.Data.SqlClient. So what was going on?
The Solution
The errant line of code throwing an exception was some initialisation code for Automapper
public static void ConfigureAutoMapperServices(this IServiceCollection services)
{
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
}
It would appear that the AppDomain.CurrentDomain.GetAssemblies() was causing the issue. After some searching I found this article and this one. So I think that there is a bug in version 5.x of Microsoft.Data.SqlClient and this is/was causing the issue.
The solution? To explicitly add a nuget for version 6.x.x of Microsoft.Data.SqlClient to the project