1

In asp.net I want to give the customer an immediate response and close the connection, and then continue execution which may be lengthy and display unimportant messages. But none of this should be visible to the customer.

I already tried Response.Flush / Close / End, CompleteRequest and anonymous delegates, but couldn't get it to work with any of this.

Sample:

Response.Write("time: "+HttpContext.Current.Session["test"]);

MagicallyEndReponse(); //But how?

Thread.Sleep(10000);                                 //Customer should not experience any delay
HttpContext.Current.Session["test"] = DateTime.Now;  //This should be available when reloading 15s later
Response.Write("BORING INFO!");                      //Customer should not see this
5
  • 1
    You shouldn't do lengthy work within a HTTP request. It should happen in an external service which can be queried for progress. Commented Jul 29, 2014 at 11:49
  • What you trying to achieve should be done with an AJAX call Commented Jul 29, 2014 at 11:51
  • Actually I'm just sending out 2 confirmation mails. Most of the time that's fast. Unless our mail server is having hiccups again. Do I really need a separate background service for this? (@Steven: Not sure how AJAX can help here. This is all server-side.) Commented Jul 29, 2014 at 12:16
  • 1
    @leuk98743 if you want to segregate your email service from your website then yes they should be separated. At the moment, you have a scenario where if you do some work in the request but then crash as you are sending those notifications then the recipients will never receive them. If you had a separate service handling the emails then it's more likely that you will have A. better throughput on your website & B. a more resilient notification service. Commented Jul 29, 2014 at 12:30
  • @James: Ok. I'll probably do that. Since this is more a solution to my problem than to my question on this page, I'll leave this question open for a few more days, to see if anything new arrives before (probably) marking your answer as accepted. Thanks. Commented Jul 29, 2014 at 14:23

1 Answer 1

2

I wouldn't recommend background thread processing in an ASP.NET application, it's not what ASP.NET or IIS is designed for.

My advice would be look at having a separate service (e.g. an internal Windows Service) which picks up work from the website and processes it, this would allow you to write a more robust multi-threaded application. You could use a durable messaging system like MSMQ / NServiceBus to pass messages to / from the service (this would mean no work is lost if the website happened to go down or restart).

The natural response for these types of request would be 202 Accepted and then possibly exposing an API for the client to query to check on the progress.

Sign up to request clarification or add additional context in comments.

Comments

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.