Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1049331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:35:56+00:00 2026-05-16T16:35:56+00:00

My Silverlight 4 application keeps in contact with a server side through a wcf

  • 0

My Silverlight 4 application keeps in contact with a server side through a wcf service. Whenever the user refreshes, navigates away or terminates the browser I should do some cleanup towards the server side.

I can not use the Application Exit event; my wcf client is dead before it eventually gets called. I can not use the (new in SL4) FrameworkElement Unloaded event; it ain’t called when the Silverlight app shuts down.

So, how do I detect the browser refresh, newpage or shutdown in time to do my cleanup?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:35:57+00:00Added an answer on May 16, 2026 at 4:35 pm

    BaBu,

    I do this exact thing when a user navigates away from my Silverlight app (or does a refresh). Follow the steps below to catch this event.

    1.) Start by listening for the HTML page’s “onbeforeunload” event, like so…

    public void Application_Startup(object sender, StartupEventArgs e)
    {
        bool ok = HtmlPage.Window.AttachEvent("onbeforeunload", Application_BeforeExit);
        ok = HtmlPage.Document.AttachEvent("onbeforeunload", Application_BeforeExit);
        MainPage mainPage = new MainPage();
        base.RootVisual = mainPage;
    }
    

    2.) Implement Application_BeforeExit() to setup and call an ASP.NET “PageMethod”, like so…

    private void Application_BeforeExit(object sender, HtmlEventArgs args)
    {
        string methodName = "ModelShutdown";
        params object[] args = new Guid().ToString());;
    
        try
        {
            ScriptObject pageMethods = (ScriptObject)HtmlPage.Window.GetProperty("PageMethods");
            if (pageMethods == null)
                throw new ArgumentException("Web page does not support PageMethods");
            object[] pageMethodArgs = { new PageMethodEventHandler(Success), new PageMethodEventHandler(Failure), null/*userContext*/};
            object[] combinedArgs = new object[args.Length + pageMethodArgs.Length];
            args.CopyTo(combinedArgs, 0);
            pageMethodArgs.CopyTo(combinedArgs, args.Length);
            pageMethods.Invoke(methodName, combinedArgs);
        }
        catch (Exception ex)
        {
            //ex.Alert();
        }
    }
    

    3.) Add the PageMethod to your page code behind (Index.aspx.cs), like so,

    public partial class Index : Page
    {
        [WebMethod] // a PageMethod called from Silverlight
        public static void ModelShutdown(string identifier)
        {
            System.Diagnostics.Debug.WriteLine("*** Signing Off: " + identifier);
        }
    }
    

    4.) Allow PageMethods on your page (Indx.aspx), like so,

    <asp:ScriptManager runat="server" EnablePageMethods="true" />
    <div id="silverlightControlHost">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
    

    Good luck,
    Jim McCurdy, YinYangMoney.com

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Silverlight application communicating with the server side through WCF services. Initially
I have a Silverlight 3 application which needs to call WCF service. The WCF
I have a Silverlight application that uploads files in chunks to a WCF service.
I have a Silverlight application that communications with an ASP.NET backend through WCF. I
I have a silverlight class library (not a top-level application) with several user controls
In my silverlight application I have two wcf services, that used to be one,
I have a Silverlight application that uses WCF services and also uses the Wintellect
I was ask to write a silverlight client - server application. One of the
I've got a working Silverlight/WCF application that I need to start thinking about scaling.
Trying to get a list of drives from a server side application using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.