I have added hosted service my .net core 3.1 project. but after add hosted service i am facing below error.
Cannot consume scoped service
. before this my project working fine. i want to add background service for my project fetching data from database.
startup.cs
services.AddScoped<IScheduleService, ScheduleService>();
services.AddHostedService<TimedHostedService>();
SMhostedService.cs
internal class TimedHostedService : IHostedService, IDisposable
{
private readonly Microsoft.Extensions.Logging.ILogger _logger;
private Timer _timer;
private IScheduleService _scheduleService;
public TimedHostedService(ILogger<TimedHostedService> logger, IScheduleService scheduleService)
{
_logger = logger;
_scheduleService = scheduleService;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Timed Background Service is starting.");
_timer = new Timer(DoWork, null, TimeSpan.Zero,
TimeSpan.FromSeconds(10));
return Task.CompletedTask;
}
private void DoWork(object state)
{
_logger.LogInformation("Timed Background Service is working.");
DateTime todaydate = new DateTime();
var getAttendanceStatus = _scheduleService.GetAttendanceStatus(todaydate.Date);
AttendanceStudent obj = new AttendanceStudent();
var ss = _scheduleService.AddAttendanceStudent(obj);
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Timed Background Service is stopping.");
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}
IScheduleService.cs
public interface IScheduleService
{
Task<int> AddAttendanceStudent(AttendanceStudent attendanceStudent);
Task<AttendanceStudent> GetAttendanceStatus(DateTime todaydate);
}
ScheduleService.cs
public class ScheduleService : IScheduleService
{
private readonly IGenericRepository<AttendanceStudent> _studentattendance;
private readonly IGenericRepository<AttendanceStaff> _staffattendance;
private readonly IGenericRepository<SchoolDetails> _schoolDetails;
public ScheduleService(IGenericRepository<AttendanceStudent> studentattendance, IGenericRepository<AttendanceStaff> staffattendance, IGenericRepository<SchoolDetails> schoolDetails)
{
_studentattendance = studentattendance;
_staffattendance = staffattendance;
_schoolDetails = schoolDetails;
}
public async Task<AttendanceStudent> GetAttendanceStatus(DateTime todaydate)
{
var ss = await _studentattendance.FindFirstByAsync(x => x.AttendanceDate == todaydate).ConfigureAwait(false);
return ss;
}
public async Task<int> AddAttendanceStudent(AttendanceStudent attendanceStudent)
{
var studentResult = await _studentattendance.FindFirstByAsync(x => x.StudentId == attendanceStudent.StudentId && x.ClassDetailsId == attendanceStudent.ClassDetailsId && x.AttendanceDate == attendanceStudent.AttendanceDate).ConfigureAwait(false);
if (studentResult == null)
{
await _studentattendance.InsertAsync(attendanceStudent);
return attendanceStudent.AttendanceStudentId;
}
else
{
studentResult.AttendanceType = attendanceStudent.AttendanceType;
await _studentattendance.UpdateAsync(studentResult).ConfigureAwait(false);
return studentResult.StudentId;
}
}
}