1

I have WCF server and Silverlight client using pollingDuplexHttpBinding binding .

I wanna close the connection and call the EndSession operation method , which clears the user from

active users list, and close the session ( IsTerminating = true )

    [OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = true)]
    void EndSession();

according to this , you can't call on wcf operation on Application_Exit event , it also gives a

solution which seems "2 much noisy" to me ,

What's my options here? Is this the only way?

1) Using the link solution?

2) Server running a method every X seconds to check duplex object state is alive?

(((ICommunicationObject)clientContract.Value).State != CommunicationState.Opened 

3) Other ?! Easy built-in solution ? Why Silverlight is HELL ?!

Zakos
  • 1,492
  • 2
  • 22
  • 41

1 Answers1

0

This is the solution which I just tried out and which works. Its core was posted in the comments in the link from your question! :)

Silverlight App.Exit event:

    private void Application_Exit(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(App.SessionId))
            return;

        var page = HtmlPage.Document.DocumentUri;
        UriBuilder builder = new UriBuilder();
        builder.Scheme = page.Scheme;
        builder.Host = page.Host;
        builder.Port = page.Port;
        builder.Path = page.LocalPath;

        string request = builder.Uri.ToString();

        request += "?closing=" + App.SessionId;
        System.Windows.Browser.ScriptObject obj = System.Windows.Browser.HtmlPage.Window.CreateInstance("XMLHttpRequest"); 
        obj.Invoke("open", "POST", request, false);

        obj.Invoke("setRequestHeader", "Content-Type", "application/x-www-form-urlencoded");

        obj.Invoke("send", "");
    }

The above code sends a request to the page that hosts the Silverlight object, which is an ASPX page and has the following code-behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(this.Request.QueryString["closing"]))
            chatSvc.Quit(this.Request.QueryString["closing"]);
    }

chatSvc should, obviously, be a reference to your service and Quit whatever method you want to call whenever a client closes. You can pass required parameters via query string.

It's not pretty, I admit, but it does work.

EDIT: The reason I'm not using DocumentUri directly, is because I'm using the navigation framework for my Silverlight application.

MBender
  • 5,395
  • 1
  • 42
  • 69