简体   繁体   English

asp.net:停止响应,继续执行

[英]asp.net: Stop response, Continue execution

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. 在asp.net中,我想给客户一个立即的响应并关闭连接,然后继续执行,这可能很长,并且显示不重要的消息。 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. 我已经尝试过Response.Flush / Close / EndCompleteRequest和匿名委托,但是无法使其与任何这些配合使用。

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

I wouldn't recommend background thread processing in an ASP.NET application, it's not what ASP.NET or IIS is designed for. 我不建议在ASP.NET应用程序中进行后台线程处理,这不是ASP.NET或IIS设计的目的。

My advice would be look at having a separate service (eg 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. 我的建议是看有一个单独的服务(例如内部Windows服务)从网站上接您的工作并进行处理,这将使您编写更健壮的多线程应用程序。 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). 您可以使用耐用的消息传递系统(例如MSMQ / NServiceBus)将消息传递到服务或从服务传递消息(这意味着如果网站发生故障或重新启动,不会丢失任何工作)。

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. 这些类型的请求的自然响应将被接受202 ,然后可能公开一个API供客户端查询以检查进度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM