Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add configuration services in program.cs in ASP.NET Core 6

One of configuration's in infrastructure layer

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));
    }
}

program.cs

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

This way doesn't work in .NET 6.

I have red warning with IServiceCollection services instance

Exception:

The name 'services' does not Exist in the current context

like image 383
Mardibak Avatar asked Oct 20 '25 02:10

Mardibak


1 Answers

The service collection is available via the WebApplicationBuilder's Services property:

CustomerManagementConfigure.Configure(builder.Services, conString);
ProductManagementConfigure.Configure(builder.Services, conString);
like image 75
Mike Brind Avatar answered Oct 21 '25 15:10

Mike Brind