c# - 升级到 .NET 8 后 Scrutor 停止工作

标签 c# .net-8.0 scrutor

我最近将 .NET 7 Web API 项目升级到 .NET 8,并且在 Scrutor 版本 4.2.2 中遇到了问题。该应用程序在升级之前工作正常,但现在我面临以下错误:

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.'

程序.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();

其他信息:

  • Scrutor 版本:4.2.2
  • .NET 版本:8
  • Microsoft.Data.SqlClient 版本:5.0.0.0

Scrutor 4.2.2 和 .NET 8 之间是否存在任何已知的兼容性问题?

任何人都可以通过提供指导来帮助我

最佳答案

这是一个known problem in Microsoft.Data.SqlClient这将在下一个版本 5.2.0 中修复。同时,您必须安装 5.2.0 的最新预览版。目前是 5.2.0-preview3.23201.1。根据其中一条评论,它可能适用于 5.1.2,但真正的修复是在 5.2 中。

导致该问题的原因是 Microsoft.Data.SqlClient 使用了不再有效的性能黑客

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.

关于c# - 升级到 .NET 8 后 Scrutor 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77568822/

相关文章:

c# - 如何在当前代码中限制给定方法/类的使用

c# - #Dotnet EF Core 迁移问题外键约束格式不正确

c# - 将 C 赋值/条件语句转换为 C#

visual-studio - 如何将.NET 7++添加到Visual Studio 2022++作为目标框架?

c# - Visual Studio 17.8,我没有看到 .NET 8.0 作为 Blazor Server 项目模板的选项

asp.net-core - 如何在类似 StructureMap 的 Scrutor 中注册程序集上的所有接口(interface)

azure - 从 ASP.NET Core 填充 Application Insights 中的用户 ID 字段

c# - 从 .NET Windows 服务运行 Rake