You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 3.1迁移至.NET 6遇CS1061错误:IServiceCollection无Decorate方法

解决.NET 6迁移中IServiceCollection缺少Decorate方法的问题

问题原因

Decorate是Scrutor库提供的扩展方法,并非.NET框架原生API。出现该错误的核心原因有两个:

  • 项目未安装适配.NET 6的Scrutor NuGet包,或包版本过低
  • 代码文件缺少Scrutor的命名空间引用

解决方案

1. 安装/更新Scrutor包

通过NuGet包管理器安装最新稳定版Scrutor,或执行.NET CLI命令:

dotnet add package Scrutor

注意:旧版本Scrutor可能存在.NET 6兼容性问题,建议升级至3.0及以上版本。

2. 添加命名空间引用

在ConfigureServicesExtensions.cs文件顶部添加Scrutor命名空间:

using Scrutor;

验证修改后的代码

完整代码示例如下:

using Scrutor;

private static void RegisterCommandHandlers(IServiceCollection serviceCollection)
{
    serviceCollection.Scan(scan => scan.FromAssemblyOf<ICommand>()
        .AddClasses(classes => classes.AssignableTo(typeof(ICommandHandler<>))
            .Where(_ => !_.IsGenericType))
        .AsImplementedInterfaces()
        .WithTransientLifetime());
    serviceCollection.Decorate(typeof(ICommandHandler<>), typeof(CommandHandlerLoggingDecorator<>));
}

内容的提问来源于stack exchange,提问作者tahir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:06:32