Like to call WCF service during the application_Exit event for silverlight application
-
vendredi 21 septembre 2012 16:18
Hi,
On this project, I play video and track the times for the video. Here is my code..
public partial class App : Application
{
MainPage mainPage;
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
mainPage = new MainPage();
//HtmlPage.RegisterScriptableObject("myApp", mainPage);
this.RootVisual = mainPage; // new MainPage();
}
private void Application_Exit(object sender, EventArgs e)
{
//mainPage.CloseBrowser();
try
{
mainPage.BrowserEndTime = DateTime.Now;
PnVx.svcDB.IsvcPnVxDataClient _svc = new PnVx.svcDB.IsvcPnVxDataClient();
_svc.UpdateAsync(mainPage.EpicID, mainPage.DAT, mainPage.MRN, mainPage.BrowserStartTime, mainPage.StartTime, mainPage.EndTime, mainPage.BrowserEndTime, mainPage.Duration);
}
catch (Exception er)
{
HtmlPage.Window.Alert(er.ToString());
}
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
When I close the browser before finishing the video, the highlighted statement appears to be executing, however, I do not see the record get updated in the database..I also put the break point in the Update method of the service, but the debugger did not stop on this method either. Moreover, when I play the video all the way, then it is updating the record.
Am I missing something?
Once again thanks for your help.
Toutes les réponses
-
vendredi 21 septembre 2012 18:59
Seems like calling the service from the Application.Exit event doesn't work at this time. I'm assuming it is because of the asynchronous nature of service calls in Silverlight, which results in the calling thread to be brought down along with the application. There are a couple of threads related to this issue at possible workarounds at http://stackoverflow.com/questions/3228932/call-webservice-when-silverlight-exit and http://forums.silverlight.net/t/20279.aspx/1http://www.duffboy.com
- Marqué comme réponse Sameem - vendredi 21 septembre 2012 21:16
-
vendredi 21 septembre 2012 21:17
I knew the that was the cause but I did not know how to solve it. Now, I know.
Thanks for the link..
- Modifié Sameem - vendredi 21 septembre 2012 21:18

