0

In response to a http request to an endpoint I have to basically make some computations, db calls, etc. and return the response to the requester. Out of the computations some of them can be made after the response is being sent and for my case it makes sense to return the response as fast as possible.

From what I've seen in ASP.NET once you return the response the whole request-response cycle is over. Is what I need achievable with basic code without using a job queue system or anything like that? I've looked into using Task and fire an async operation before sending the response back, but from what I've been reading it can be dangerous, the system might terminate the process randomly and such.

I'm very much used to node/express and I'm trying to do the equivalent of:

router.get('/myroute', executeStuff, returnResponse, executeStuff2);

// initial processing
executeStuff = (req, res, next) => {
  someAsyncOperation()
    .then(() => next())
    .catch(...);
}

// send response after necessary stuff is done
returnResponse = (req, res, next) => {
  res.send(responseBody);
  next();
}

// do the after response processing, side effects, etc.
executeStuff2 = (req, res, next) => {
  someAsyncOperation()
    .then(() => {
      // end of the chain
    })
    .catch(...);
}
6
  • Well, as a general rule, that client web browser might have been closed. Or now the user clicked on a link and is visiting their banking site. (you going to get to continue to communicate with the client side then? - not!!!). However, if you want some kind of real time com channel such as a chat bot, or some other live connection to the user? Then their are frameworks for that. you can adopt signalR for this purpose, and you be free to send lots and lots of messages to the client side at your beck and call. Commented Oct 14, 2021 at 5:01
  • check out: learn.microsoft.com/en-us/aspnet/signalr/overview/…. Commented Oct 14, 2021 at 5:02
  • @AlbertD.Kallal thanks for the answer, I need a default http response-request approach, but with the possibility of continuing processing after the response is being sent. Unfortunately for multiple reasons SignalR is out of the question for my case. Commented Oct 14, 2021 at 6:02
  • there is no such thing as a response then request. It ALWAYS the other way around. You make a request, and the web server responds. If you can't use signalR, then you try some sort of timer event client side. Server side start a separate task from that given web page (so the round trip is not held up on the server for that longer executing code).. You could put the timer in say a up-date panel, and have that check every 1 second some persisting value from the longer separate task that you started. This could be a web method call (ajax), and VERY likely session("some flag done") would be used. Commented Oct 15, 2021 at 1:56
  • Typo. I meant request-response. And the after getting the response the client shouldn't be concerned in any way with anything about what goes on on the server - after the response is sent we shouldn't take it into the equation anymore, that was the whole point. I ended up using a message queue, separating in multiple services and delegating the work to another service asynchronously, something I wanted to avoid at this point. Commented Oct 16, 2021 at 6:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.