Skip to content

DRNJ

Light at the end of the Technology Tunnel

  • Home
  • About
  • Contact
DRNJ

Tag: .NET Core

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

.NET Core WebAPI CORS

July 6, 2020

The Problem

How hard can it be to configure CORS in a .NET Core 3.1 WebAPI? The answer…quite hard.

The Microsoft documentation shows how to configure CORS and it should be straightforward. However, my experience and the exeperience of many others on StackOverflow have shown me that all is far from simple….Although the solution, when finally found was remarkably simple.

Code

CORS can be configured on the ConfigureServices method in Startup.cs with more or less:

services.AddCors(options =>
{
options.AddPolicy(name: "myCORSPolicy",
builder =>
{
    builder.WithOrigins(this.ApiConfiguration.CorsOrigins.ToArray());

     builder.AllowAnyMethod();
     builder.AllowAnyHeader();
     builder.AllowCredentials();
});

});

and in the Configure method

 app.UseCors("myCORSPolicy");

First Problem

So..I’m on an internal development network and I want to allow all origins (i.e. “*”) and AllowCredentials (as I want to use Active Directory).

Wrong!

The combination of .WithOrigins(“*”) and AllowCredentials is expressly forbidden and will generate a run-time exception.

Second Problem – Trailing Slashes

The .WithOrigins takes a “list” of origins i.e. URLs which can access your API. These must not have trailing slashes, e.g.

http://mydomain.comĀ  - works

http://mydomain.com/ - CORS will not allow access from this origin

Microsoft do mention this in their documentation. however, it is far from clear and easily overlooked.

 

 

.NET Core .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