using RestoranWeb.Models.Getir; using RestoranWeb.Models.GetirContext; namespace RestoranWeb.Service { using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Threading; using System.Threading.Tasks; public class TimerService : IHostedService, IDisposable { private Timer _timer; private readonly IServiceProvider _serviceProvider; public TimerService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public Task StartAsync(CancellationToken cancellationToken) { _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(5)); return Task.CompletedTask; } private void DoWork(object state) { using (var scope = _serviceProvider.CreateScope()) // bunu yapma nedeniz contrunterde context olması { // DATA İLE İLGİLİ ŞEYLERİ BURDAN OLUCAK YOKSA LİFTİME PATLIYOR. YANİ FONK. BURDAN YAZILICAK. var dbContext = scope.ServiceProvider.GetRequiredService<RestoranContext>(); Getir getir = new Getir(dbContext); getir.GetirCalistir(); } } public Task StopAsync(CancellationToken cancellationToken) { _timer?.Change(Timeout.Infinite, 0); return Task.CompletedTask; } public void Dispose() { _timer?.Dispose(); } } } |