3

基础设施层的配置之一

客户管理配置.cs

public class CustomerManagementConfigure
{
    public static void Configure(IServiceCollection services,string conString)
    {
        services.AddTransient<ICustomerApplication, CustomerApplication>();
        services.AddTransient<ICustomerRepository, CustomerRepository>();

        services.AddDbContext<DiscountContext>(x => x.UseSqlServer(conString));
    }
}

程序.cs

var conString = builder.Configuration.GetConnectionString("CustomerDB");
CustomerManagementConfigure.Configure(services, conString);
ProductManagementConfigure.Configure(services, conString);

这种方式在 .NET 6 中不起作用。

IServiceCollection services我对实例有红色警告

例外:

当前上下文中不存在名称“服务”

4

1 回答 1

2

服务集合可通过WebApplicationBuilderServices属性获得:

CustomerManagementConfigure.Configure(builder.Services, conString);
ProductManagementConfigure.Configure(builder.Services, conString);
于 2022-01-11T15:55:04.540 回答