Skip to content

DRNJ

Light at the end of the Technology Tunnel

  • Home
  • About
  • Contact
DRNJ

Month: April 2025

AutoMapper and “Could not load type ‘SqlGuidCaster'” Error

April 7, 2025

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

Uncategorized .NET Core

Recent Posts

  • AutoMapper and “Could not load type ‘SqlGuidCaster'” Error
  • OpenVPN on Docker and the Strange Error Message Saga
  • Docker CLI and Compose Information Message
  • Docker Containers and Azure – An Introduction
  • Serilog in .Net Core 6

Recent Comments

    Archives

    • April 2025
    • December 2024
    • April 2024
    • September 2022
    • November 2021
    • June 2021
    • March 2021
    • July 2020
    • April 2020
    • November 2019
    • September 2019
    • July 2019
    • May 2019
    • February 2019
    • July 2018
    • June 2018

    Categories

    • .NET Core
    • Azure
    • Docker
    • DotNet
    • Security
    • Uncategorized
    • WebAPI
    • Windows

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org

    Idealist by NewMediaThemes