【问题标题】:Add configuration services in program.cs in ASP.NET Core 6在 ASP.NET Core 6 的 program.cs 中添加配置服务
【发布时间】:2022-01-12 02:02:19
【问题描述】:

基础设施层的配置之一

CustomerManagementConfigure.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 实例有红色警告

例外:

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

【问题讨论】:

    标签: c# asp.net-core razor-pages asp.net-core-6.0


    【解决方案1】:

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

    CustomerManagementConfigure.Configure(builder.Services, conString);
    ProductManagementConfigure.Configure(builder.Services, conString);
    

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 2022-08-23
      • 1970-01-01
      • 2021-11-22
      • 2016-10-19
      • 2022-06-13
      • 2022-11-22
      • 2022-11-23
      • 1970-01-01
      相关资源
      最近更新 更多