Reputation: 1590
I have ImportExportService.
In StartUp class in method ConfigureServices I use it as
services.AddImportExportService(Configuration.GetConnectionString("DefaultConnection"));
Extential method AddImportExportService:
public static class IServiceCollectionExtension
{
public static IServiceCollection AddImportExportService(this IServiceCollection services,
string connString,
ILogger<ImportExportService> logger
)
{
services.AddTransient<IImportExportService, ImportExportService>(provider => new ImportExportService(connString));
return services;
}
}
ExportImportService uses logging.
I tried to inject Logging in service as param in constructor like ILoger<ImportExportService> logger
, but constructor includes only one param and extension method AddImportExportService get error.
How inject Logging in ExportImportService? Thank you
Upvotes: 0
Views: 2171
Reputation: 12610
services.AddTransient<IImportExportService, ImportExportService>(provider => new ImportExportService(connString));
should be
services
.AddTransient<IImportExportService, ImportExportService>(
provider => new ImportExportService(connString, provider.GetRequiredService<ILogger<ImportExportService>>()));
assuming the constructor of ImportExportService
has two arguments. Then the extension needs only two arguments:
public static IServiceCollection AddImportExportService(
this IServiceCollection services,
string connString)
Upvotes: 5