从依赖容器在 .NET 8 的 Program.cs 文件中添加 IoC

问题描述 投票:0回答:1

这是我在依赖容器中的 IoC 方法

public class DependencyContainer
{
    public static void RegisterServices(IServiceCollection service)
    {
        #region Services Injection

        service.AddScoped<IUserService, UserService>();

        #endregion

        #region Repositories Injection

        service.AddScoped<IUserRepository, UserRepository>();

        #endregion
    }
}

现在我想将其添加到 web Layer 的 Program .cs 中。 我如何在 .NET 8 中做到这一点?

在 .NET 5 中是:

    public static void RegisterServices(IServiceCollection services)
 {
  DependencyContainer.RegisterServices(services);
  }

但是在.NET 8中我们没有公共方法,我该怎么做?

c# asp.net-core dependency-injection dependencies .net-8.0
1个回答
0
投票

WebApplicationBuilder
WebApplication.CreateBuilder
返回的
Services
类型的属性
IServiceCollection
完全用于 DI 服务注册,因此:

DependencyContainer.RegisterServices(builder.Services);

迁移到 6.0 文档中对此进行了介绍:

© www.soinside.com 2019 - 2024. All rights reserved.