web services - Using events to trigger ActionResult in ASP.NET MVC -
i working on asp.net mvc web service. in web page, when user clicks on button, triggers complex method takes bit of time finish. want redirect user waiting page , then, when process finished, redirect user new page.
when process done raises event, can listen controller. cannot make last step work (the controller redirecting new page upon receiving event).
here naïve attempt @ doing (with simpler names):
public mycontroller() { eventscontrollerclass.processcomplete += new eventhandler<myargsclass>(oneventreceived); } private void oneventreceived(object sender, myargsclass eventarguments) { redirecttopage(); } private actionresult redirecttopage() { return redirecttoaction("pagename"); }
after many days working on this, have viable solution. may not pretty, works, , maybe ideas can useful other people, here goes:
i explain solution particular problem: need button redirect "waiting" page while longer process runs in background , raises event when finished. when event received, want redirect user (automatically) final page.
first, created class listen event. tried doing directly in controller, need careful signing , unsigning, because apparently controllers created , destroyed @ each request. in "listener class" have bool property set "true" when event received.
when first action triggered, controller redirects "wait" page, have simple java script redirecting new action:
<script type="text/javascript"> window.location = "@url.action("waitthenredirect", "auxiliarycontrollername")"; </script> this sets in motion long process (through event). key asynchronous action (this controller inherits asynccontroller). (note used auxiliary controller. keep asynchronous stuff apart.) how looks (more info here):
public static event eventhandler<auxiliaryeventsargs> processready; public void waitthenredirectasync() { asyncmanager.outstandingoperations.increment(); processready += (sender, e) => { asyncmanager.parameters["success"] = e.success; asyncmanager.outstandingoperations.decrement(); }; waitforevent(); } public actionresult waitthenredirectcompleted(bool success) { if (success) { return redirecttoaction("redirecttoview", "controllername"); } else { return redirecttoaction("unexpectederror", "controllername"); } } private void waitforevent() { bool iswaitsuccessful = true; int waitingloops = 0; int waitingthreshold = 200; int sleepperiod = 100; // (milliseconds) while (!eventslistener.isthethingready()) { system.threading.thread.sleep(sleepperiod); ++waitingloops; if (waitingloops > waitingthreshold) { system.diagnostics.debug.writeline("waiting timed out!"); iswaitsuccessful = false; break; } } iswaitsuccessful = true; if (null != processready) { auxiliaryeventsargs arguments = new auxiliaryeventsargs(); arguments.success = iswaitsuccessful; try { processready(null, arguments); } catch (exception ex) { system.diagnostics.debug.writeline("error in event processready" + ex); } } } i believe possible use ajax syntax alternative solutions, have , works nicely. believe not common need, benefit!
Comments
Post a Comment