This is my program.cs
using TestEmailTaskScheduler.Controllers;
using TestEmailTaskScheduler.Models;
using System.Configuration;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddSingleton<EmailService>();
builder.Services.AddHostedService<EmailService>();
builder.Configuration.GetSection("DailyEmail").Get<MySettingsModel>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
And this is a part of the test code in my controller service:
using Microsoft.Extensions.Options;
using TestEmailTaskScheduler.Models;
using System.Data;
using System.Net;
using System.Net.Mail;
using System.Configuration;
namespace TestEmailTaskScheduler.Controllers
{
public class EmailService : BackgroundService
{
private const int generalDelay = 1 * 10 * 1000; // 10 seconds
private readonly IOptions<MySettingsModel> appSettings;
private readonly IOptions<MyDatabaseModel> databaseSettings;
private Timer Schedular;
public EmailService(IOptions<MySettingsModel> app)
{
appSettings = app;
}
public EmailService(IOptions<MyDatabaseModel> dbs)
{
databaseSettings = dbs!;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(generalDelay, stoppingToken);
await DoBackupAsync();
}
}
private Task DoBackupAsync()
{
// here i can write logic for taking backup at midnight
if (IsMidnight())
{
Console.WriteLine("Executing background task");
//SendEmail();
}
return Task.FromResult("Done");
}
Error: System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: TestEmailTaskScheduler.Controllers.EmailService Lifetime: Singleton ImplementationType: TestEmailTaskScheduler.Controllers.EmailService': Unable to activate type 'TestEmailTaskScheduler.Controllers.EmailService'. The following constructors are ambiguous: Void .ctor(Microsoft.Extensions.Options.IOptions
1[TestEmailTaskScheduler.Models.MySettingsModel]) Void .ctor(Microsoft.Extensions.Options.IOptions1[TestEmailTaskScheduler.Models.MyDatabaseModel])) (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: TestEmailTaskScheduler.Controllers.EmailService': Unable to activate type 'TestEmailTaskScheduler.Controllers.EmailService'. The following constructors are ambiguous: Void .ctor(Microsoft.Extensions.Options.IOptions1[TestEmailTaskScheduler.Models.MySettingsModel]) Void .ctor(Microsoft.Extensions.Options.IOptions1[TestEmailTaskScheduler.Models.MyDatabaseModel]))'
Why is this happening? I thought it is already called in the program.cs?
EmailService(IOptions<MySettingsModel> app, IOptions<MyDatabaseModel> dbs){...}.GetSection("DailyEmail").Get<MySettingsModel>()will return the config object. What you want is.Configure<MySettingsModel>(....GetSection("DailyEmail"))which you should see in other examples / tutorials.