Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load type 'SqlGuidCaster' from assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5'

I created a blank webapi project for .NET8,Install two packages Microsoft.Extensions.DependencyModel and Microsoft.Data.SqlClient. The following is the source code:

using Microsoft.Extensions.DependencyModel;
using System.Reflection;
using System.Runtime.Loader;

var builder = WebApplication.CreateBuilder(args);
var list = DependencyContext.Default.RuntimeLibraries.ToList();
var assemblies = list
    .Select(m =>
    {
        try {
            return AssemblyLoadContext.Default
                .LoadFromAssemblyName(new AssemblyName(m.Name));
        }
        catch { return null; }
    })
    .Where(m => m != null)
    .ToList();

foreach (var assembly in assemblies) {
    foreach (var type in assembly.GetTypes()) { }
}

app.Run();

After compilation, an error message such as the title will be prompted. This does not exist in .NET6. A similar error will occur when changing Microsoft.Data.SqlClient to System.Data.SqlClient. How to solve it? Thank you so much.

like image 301
Jin Lyu Avatar asked Dec 15 '25 10:12

Jin Lyu


1 Answers

This is due to a bug in Microsoft.Data.SqlClient and as mentioned here, the workaround for now seems to be to update the Microsoft.Data.SqlClient NuGet package to the latest preview version, which is currently v5.2.0-preview3.23201.1. I had the same issue and can confirm, that this workaround is working.

like image 71
Aho Samuel Avatar answered Dec 17 '25 00:12

Aho Samuel