Scrutor stopped working after upgrading to .NET 8

710 Views Asked by At

I recently upgraded my .NET 7 web API project to .NET 8, and I'm encountering an issue with Scrutor version 4.2.2. The application was working fine before the upgrade, but now I'm facing the following error:

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, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field.'

Program.cs :

using Scrutor;
using WebApplication1.Model;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
builder.Services.Scan(scan => scan.FromApplicationDependencies()
                    .AddClasses(classes => classes.InExactNamespaces("WebApplication1.Model"))
                    .UsingRegistrationStrategy(RegistrationStrategy.Skip)
                    .AsImplementedInterfaces()
                    .WithTransientLifetime()
                );
//builder.Services.AddScoped<SaqmmdbContext,SaqmmdbContext>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Additional Information:

  • Scrutor Version: 4.2.2
  • .NET Version: 8
  • Microsoft.Data.SqlClient Version: 5.0.0.0

Are there any known compatibility issues between Scrutor 4.2.2 and .NET 8?

Can anyone please help me here by providing their guidance

2

There are 2 best solutions below

2
Panagiotis Kanavos On

This is a known problem in Microsoft.Data.SqlClient that will be fixed in the next version, 5.2.0. In the meantime you'll have to install the latest preview of 5.2.0. At the moment it's 5.2.0-preview3.23201.1. According to one of the comments it may work with 5.1.2, but the real fix is in 5.2.

The problem was caused because Microsoft.Data.SqlClient used a performance hack that no longer works

We've got a situation where the runtime was previously using struct SqlGuid{ byte[] _value; } and that has changed to struct SqlGuid{ Guid? _value; }. overlaying a struct with ref types in the same position was unsafe and icky but supported. In NET8 this changes to trying to overlay a value type and a ref type which is entirely illegal and causes the type loader to abort the assembly load.

0
Asunez On

I stumbled upon the same error. In my case going with a prerelease package was not an option, so I had to look for another solution. After some debugging I discovered that assemblies that were fed to Scrutor contained also .dlls from NuGet packages (and among them was the Microsoft.Sql.DataClient package). After limiting the .dlls to only the ones I was interested in (from my solution), it works fine.