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

.NET 6中调用AddInfrastructureShared()方法找不到的问题求助

解决.NET 6中找不到AddInfrastructureShared()扩展方法的问题

你定义的扩展方法代码:

public static class DependencyInjection
{
    public static IServiceCollection AddInfrastructureShared(this IServiceCollection services, IConfiguration config)
    {
        services.Configure<MailSettings>(config.GetSection("MailSettings"));
        services.AddTransient<IDateTime, DateTimeService>();
        services.AddTransient<IEmailService, EmailService>();
        services.AddTransient<ICsvFileBuilder, CsvFileBuilder>();

        return services;
    }
}

调用时报错的代码:

//not find AddInfrastructureShared()
builder.Services.AddInfrastructureShared();

针对这个问题,按以下步骤排查解决:

  • 补充方法参数
    你的扩展方法定义需要两个参数:IServiceCollection和IConfiguration,但调用时只传入了前者。在.NET 6的top-level语句中,需要传入builder.Configuration作为第二个参数,修正后的调用代码:

    builder.Services.AddInfrastructureShared(builder.Configuration);
    
  • 添加命名空间引用
    确保调用代码所在的文件,已经引用了DependencyInjection类所在的命名空间。比如类在YourProject.Infrastructure命名空间下,就添加:

    using YourProject.Infrastructure; // 替换为实际命名空间
    
  • 确认项目引用
    如果DependencyInjection类在单独的类库项目中,检查Web项目是否已添加该类库的项目引用。右键Web项目→添加→项目引用,勾选对应的类库即可。

  • 验证访问修饰符
    确保DependencyInjection类是public static,且AddInfrastructureShared方法是public static,你当前的代码已经满足这一点,若之前有修改过访问级别,需恢复为public。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 03:23:15