【问题标题】:Silverlight calling WCFSilverlight 调用 WCF
【发布时间】:2011-01-04 18:57:30
【问题描述】:

我是 silverlight 的新手,只是想知道在 Silverlight 中调用 WCF 服务所涉及的步骤是否与在 asp 中相同或有什么不同。如果有任何不同,请帮助我。

提前致谢。

【问题讨论】:

    标签: silverlight wcf silverlight-4.0


    【解决方案1】:

    您需要注意的主要事项是

    1. silverlight 仅支持基本的httpbinding
    2. 您需要在 wcf 主机服务器的根文件夹中有 clientaccesspolicy.xml 文件或 crossdomain.xml,然后才能成功从 silerlight 调用 web 服务

    http://msdn.microsoft.com/en-us/library/cc197955%28v=vs.95%29.aspx

    【讨论】:

      【解决方案2】:

      我得到了答案,asp和silverlight之间有细微的差别。 在 silverlight 中,所有服务调用都是异步的,因此您必须有一个事件处理程序,以便在异步调用完成时获取数据。

      只是一个小例子

      当您从 asp 调用 WCF 服务时,您使用

      proxy_http.FunctionClient fc = new proxy_http.FunctionClient();
              txtDisplay.Text = fc.Add(Convert.ToInt32(txtFirst.Text),Convert.ToInt32(txtSecond.Text)).ToString();
      

      如果是 Silverlight,您将使用

      private void Add_Click(object sender, RoutedEventArgs e)
          {
              proxy_htt.FunctionClient fc = new proxy_htt.FunctionClient();
              fc.AddCompleted += new EventHandler<proxy_htt.AddCompletedEventArgs>(fc_AddCompleted); 
              fc.AddAsync(Convert.ToInt32(txtFirst.Text),Convert.ToInt32(txtSecond.Text));
      
          }
      
          void fc_AddCompleted(object sender, proxy_htt.AddCompletedEventArgs e)
          {
              txtResult.Text = e.Result.ToString();
          }
      

      【讨论】:

      • 您也必须使用 clientaccesspolicy.xml 文件或 crossdomain.xml
      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多