ASP.NET execute long running method in the background
What I'm trying to accomplish: I want to call a long running business logic method from the code behind of an asp.net page (on a button click from the user) and have the process run asynchronously. The process has some 开发者_开发知识库business objects as parameters. Once the process kicks off the user should be displayed a screen indicating that they will be emailed the results; the important thing here is that the user not have to wait for the process to complete. What is the best way to do this? The process already exists and works in my project but the page just hangs while it runs.
Originally I thought about moving the method to a web service. Because it would require an extensive list of parameters I was going keep the signature the same and pass the web service business objects but I'm getting namespace errors. Specifically "Argument type MyBusinessLogic.MyObject is not assignable to parameter type MyWebService.MyObject" even though my web site and the web service are referencing the same DLL. I get why that is happening but I need a way around it.
The actual implementation of the solution is not terribly critical. It would be awesome if there was some way to run a method call asynchronously in ASP.NET that I'm not aware of. If not is sending and XML parameter list and parsing it into the business objects my only option for the Web Service?
I have the same issue, so what I did is i moved the processing call to Pre_Render method and while it processes it calls (create) js function in the front end to update the progress.
Response.Write("fnUpdate('Processing 1 to 30 files')");
Response.Flush();
I need to change my processing logic so that it uses delegate to update the progress.
Hope it helps.
EDIT: Don't forget to increase the Script.Timeout
Server.ScriptTimeout = 3600;
Ensure that you using Async ASP.net pages.
http://blogs.msdn.com/b/tmarq/archive/2010/04/14/performing-asynchronous-work-or-tasks-in-asp-net-applications.aspx
I'm pretty sure what I ended up doing is close to Jeremy's method.
I found this site: http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx which explains how to call a method asynchronously. To sum it up: If you define a proxy and call ProxyMethod.BeginInvoke(null,null) then the method will be run with no callback i.e. fire and forget. That was all I really wanted so it's what I'm going with. I don't know if it was necessary but I took the method itself and moved it from my codebehind into the business logic and made it a static method. So by code looks like this:
private delegate void AsyncReportGen(ReportParameterObject rpo);
protected void GenerateReport()
{
//other code
AsyncReportGen reportGen = ReportGenClass.GenerateReportStaticMethod;
reportGen.BeginInvoke(rdo, null, null);
}
The GenerateReport method is called by a button click event. The GenerateReportStaticMethod fires off an email with the report attached when it is complete. The method called by BeginInvoke runs in it's own thread so the page can finish loading and show the user a message that they will receive an email. The user can then continue using the site or close their browser.
精彩评论