11

In my ASP.net web application, I have code in the Page_Init event() of my Page which checks some session variables to redirect users if a Session timeout has occurred.

In my Silverlight application I have a button event handler which executes some ESRI ArcGIS code asynchronously, and which configures an event handler which fires when the asynchronous call completes:

QueryTask queryTask = new QueryTask(myLayer.Url + "/" + layerID);
queryTask.ExecuteCompleted += new EventHandler<QueryEventArgs>(queryCountyTask_ExecuteCompleted);
queryTask.ExecuteAsync(query);

There is also a bit of code which calls a JavaScript function on-page to hide a Panel. The inclusion of this code causes a full page postback.

HtmlPage.Window.Invoke("hideReport"); 

My problem is that sometimes the queryCountyTask_ExecuteCompleted() Silvelright event fires before the Page_Init() Page event and sometimes it fires after. In those cases where the Silverlight fires before the Page event, the Session state is empty, causing the user to be incorrectly redirect to a "your session has timed-out" page. As long as the Silverlight event fires after the Page event, the Session variables are still present and everything works fine.

It looks like the placement of the Invoke method has no bearing on when the Page-level event is fired, so the order of events appears to be seemingly random. Is there a way to order the events so as to avoid these race conditions?

Is there a way to coordinate these asychronous callback events with the normal Page Lifecycle of a web application page so as to avoid these race conditions?

Cœur
  • 37,241
  • 25
  • 195
  • 267
bperniciaro
  • 901
  • 2
  • 12
  • 30

1 Answers1

1

If you flip the problem around how about getting your ASP.net page to fire the Silverlight functions after it has completed so there will be no race condition.

Here is a Link on how to make events in Silverlight scriptable from external javascript that should help.

Kevin Ross
  • 7,185
  • 2
  • 21
  • 27
  • Unfortunately it isn't Silverlight functions being called directly, but Silverlight events being fired by ESRI ArcGIS service calls (namely events that are fired either when a GIS query is successful or unsuccessful). The time it takes to issue this query dictates if it returns before the postback occurs. – bperniciaro Apr 20 '15 at 18:22
  • The technique of calling Silverlight methods from your server side should work since you can wait until the page is loaded to make the query calls. You will just need to move any code from the Silverlight code that runs on load and move it to methods that you call once the page is loaded. You are also able to pass state from the server sid eto the Silverlight plugin using the InitParams property http://weblogs.asp.net/lduveau/provide-startup-parameters-to-silverlight-with-initparams – Dave Timmins Apr 20 '15 at 20:28