private void DoWork(object state)
        {
            using var scope = Services.CreateScope();
            var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
            var message = "ConsumeScopedService. Received message at " + DateTime.Now.ToString();
            var log     = new HostedServiceLog()
            {
                Message = message
            };

            context.HostedServiceLogs.Add(log);
            context.SaveChanges();
        }
Example #2
0
 private void DoWork(object state)
 {
     using (var scope = Services.CreateScope())
     {
         var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
         var message = "ConsumeScopedService mensaje recibido a: " + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss");
         var log     = new HostedServiceLog()
         {
             Message = message
         };
         context.HotedServiceLogs.Add(log);
         context.SaveChanges();
     }
 }
Example #3
0
 private void TareaProgramada(object state)
 {
     using (var scope = Services.CreateScope())
     {
         var context          = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
         var mensaje          = "ConsumeScopedService: Escribiendo el " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss");
         HostedServiceLog log = new HostedServiceLog()
         {
             Mensaje = mensaje
         };
         context.HostedServiceLogs.Add(log);
         context.SaveChanges();
     }
 }
 private void DoWork(object status)
 {
     using (var scope = Services.CreateScope())
     {
         ApplicationDbContext context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
         string           message     = $"ConsumeScopedService: Mensaje generado {DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")}";
         HostedServiceLog log         = new HostedServiceLog()
         {
             Message = message
         };
         context.HostedServiceLogs.Add(log);
         context.SaveChanges();
     }
 }
        private void TimerCb(object state)
        {
            using (var scope = services.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();

                var log = new HostedServiceLog
                {
                    Message   = $"ConsumeScopedServiceHostedService message: {DateTime.UtcNow.ToString()}",
                    CreatedAt = DateTime.UtcNow,
                };

                context.HostedServiceLogs.Add(log);
                context.SaveChangesAsync().Wait();
            }
        }