【问题标题】:WCF & Silverlight >> pollingDuplexHttpBinding >> Calling web services from silverlight as the browser is closedWCF & Silverlight >> pollingDuplexHttpBinding >> 在浏览器关闭时从 silverlight 调用 Web 服务
【发布时间】:2012-11-11 15:13:40
【问题描述】:

我有使用 pollingDuplexHttpBinding 绑定的 WCF 服务器和 Silverlight 客户端。

我想关闭连接并调用EndSession操作方法,清除用户

活跃用户列表,并关闭会话(IsTerminating = true

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

根据this,不能在Application_Exit事件上调用wcf操作,它也给出了一个

对我来说似乎“2 很吵”的解决方案,

我在这里有什么选择?这是唯一的方法吗?

1) 使用链接解决方案?

2) 服务器每 X 秒运行一个方法来检查双工对象状态是否处于活动状态?

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

3) 其他?!简单的内置解决方案 ?为什么 Silverlight 是地狱?!

【问题讨论】:

    标签: wcf silverlight pollingduplexhttpbinding


    【解决方案1】:

    这是我刚刚尝试过并且有效的解决方案。它的核心发布在您问题链接中的 cmets 中! :)

    Silverlight App.Exit 事件:

        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", "");
        }
    

    以上代码向承载 Silverlight 对象的页面发送请求,该页面是一个 ASPX 页面,并具有以下代码隐藏:

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

    chatSvc 显然应该是对您的服务的引用,Quit 应该是您在客户端关闭时要调用的任何方法。您可以通过查询字符串传递所需的参数。

    我承认,它并不漂亮,但它确实有效。

    编辑: 我不直接使用DocumentUri 的原因是因为我正在为我的 Silverlight 应用程序使用导航框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多