如何解决无法使用范围服务

Add*_*han 5 c# .net-core asp.net-core-3.1

我已将托管服务添加到我的 .net core 3.1 项目中。但添加托管服务后我面临以下错误。

无法使用范围服务

。在此之前我的项目运行良好。我想为我的项目添加后台服务从数据库获取数据。

启动.cs

services.AddScoped<IScheduleService, ScheduleService>();
services.AddHostedService<TimedHostedService>();
Run Code Online (Sandbox Code Playgroud)

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

IScheduleService.cs

 public interface IScheduleService
    {
        Task<int> AddAttendanceStudent(AttendanceStudent attendanceStudent);
        Task<AttendanceStudent> GetAttendanceStatus(DateTime todaydate);
    }
Run Code Online (Sandbox Code Playgroud)

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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

小智 -3

尝试

services.AddSingleton<IScheduleService, ScheduleService>();
Run Code Online (Sandbox Code Playgroud)