I know this is bad design and I am trying to find any reason to not do this, so I need some help.
I have an API endpoint that is being hit and needs to respond back with an OK response within a few seconds at most. Currently we are taking the call and processing a lot of data and then returning the OK, however, if the DoLotsOfWork method times out, no OK response is being returned.
public IActionResult DoWork([FromBody] MyDTO myDto){
DoLotsOfWork();
return OK()
}
Now I've suggested that we migrate over to some sort of Queue and make the job run there but I am being told no as that work would take to long. Instead I am being told to do this.
public IActionResult DoWork([FromBody] MyDTO myDto){
try{
return OK();
}
catch{
}
finally{
DoLotsOfWork();
}
}
Does anyone know of any drawbacks by doing things this way?
Since the actual question appears to be entirely "What are the drawbacks?", and you appear to know how the
try-catch-finallyis working as hacky a way to execute code after the API responds (despite some people in the comments and answers explaining what it does), I'll just link to this very well-written StackOverflow answer to a similar question authored by Stephen Cleary.Edit 2: vatbub's answer below listing issues with that approach makes a good point in #4 (1-3 mostly reiterate
try-catchmisuse), and the description that follows elaborates.Edit: In case it disappears from the internet: