0

I'm working in an application with frontend Angular 6 and backend .net core 2.

I want that, if there is a certain circumstance, a procedure stored in the database (MySQL) is executed, which takes a few minutes to execute regardless of what the user continues to do. I do not need to collect any results from that procedure.

How do I launch it that way, to continue running regardless of what the user does, even if he closes the browser?

I have tried several ways ...

 [HttpGet]
 [Route("Check/{idCheck}")]
 public async Task ComprobarDatos(int idCheck)
 {
      try
      {
         int? timeout = _context.Database.GetCommandTimeout();
         _context.Database.SetCommandTimeout(600);
         await _context.Database.ExecuteSqlCommandAsync("call CheckProcess({0});", idCheck)
                    .ConfigureAwait(false);
         if(timeout!= null && timeout > 0)
         {
            _context.Database.SetCommandTimeout(timeout);
         }
      }
      catch (Exception ex)
      {
         _logger.LogError("Ocurrió un problema ejecutando el procedimiento CheckProcesscon id {0}: " + ex.Message, idCheck);
      }
 }

I have also tried:

public IActionResult ...
...
_context.Database.ExecuteSqlCommand("call CheckProcess({0});", idCheck);
return Ok();

And:

public IActionResult ...
...
_context.Database.ExecuteSqlCommandAsync("call CheckProcess({0});", idCheck)
.ConfigureAwait(false);
return Ok();

And:

public IActionResult ...
...
_context.Database.ExecuteSqlCommandAsync("call CheckProcess({0});", idCheck)
.ConfigureAwait(true);
return Ok();

And it always waits for the stored procedure to finish in order to continue the execution of the code.

Thank you!

fmdavid
  • 95
  • 2
  • 12
  • 1
    If you don't want to await, don't `await`. If you don't use `awai` there's no point in adding the call to `ConfigureAwait()`. That shouldn't affect whether the method blocks or not though – Panagiotis Kanavos Jul 12 '18 at 13:10
  • **On the other hand**, MySQL's Connector/NET provider *fakes* asynchronous execution by running a blocking call on another thread. If you search the source code, in the end it creates a delegate and runs it with `.BeginInvoke()` which simply runs the call on another thread. Perhaps this affects how `ConfigurAwait` behaves here? – Panagiotis Kanavos Jul 12 '18 at 13:12
  • Could be. I have tried with the provider Pomelo.EntityFrameworkCore.MySql. It works with ExecuteSqlCommand but I have to wait for the execution of the stored procedure to finish before continuing. With ExecuteSqlCommandAsync it continues executing the code, but it does not execute the stored procedure. That shows the following error:Microsoft.EntityFrameworkCore.Database.Command:Error: call CheckProcess(@p0); System.ObjectDisposedException: Cannot access a disposed object. Object name: 'MySqlConnection'. Something I'm not doing right! – fmdavid Jul 12 '18 at 18:22

1 Answers1

0

You may achive this by using a background server tasks.

  • on each request start a new background task and return
  • background task executes store procedure
Set
  • 47,577
  • 22
  • 132
  • 150
  • Thanks for the reply. That response has been the beginning of the way to resolve the issue. As it was good for me to be able to queue the work to perform in the background, I followed the indications of https://stackoverflow.com/questions/51115710/net-core-queue-background-tasks – fmdavid Jul 13 '18 at 12:34