zoukankan      html  css  js  c++  java
  • Silverlihgt Client调用WCF服务

    很多人肯定在实际的应用开发中都被Silverlight调用WCF服务的相关操作折磨了一阵子。要想很好的掌握这一应用技巧,还是需要我们从不断的操作中去积累经验。在这里我们将会为大家详细介绍一下这方面的知识。

    WCF工程中需要注意的地方:

    1.新建一个crossdomain.xml文件,内容如下

    1. < ?xml version="1.0" encoding="utf-8" ?> 
    2. < cross-domain-policy> 
    3. < allow-access-from domain="*" /> 
    4. < !-- 意为:允许来自任意域名对本web服务站点的任意跨域访问,
      如要限制跨域访问站点:可将"*"更改为相应域名,多个域名则为多个
      < allow-access-from ... />节点 --> 
    5. < /cross-domain-policy> 

    2.修改web.config文件内容

    1. < endpoint address="" binding="basicHttpBinding" 
      contract="Demo.WCF.IService1"> 
    2. < endpoint address="mex" binding="basicHttpBinding" 
      contract="IMetadataExchange"/>

    3.或app.config文件内容,增加basicHttpbinding绑定,默认的情况下可能会有其他的绑定

    注意,在同一个服务接口上绑定不同协议的时候,地址一定要不一样

    1. < endpoint address="" binding="basicHttpBinding" 
      contract="Demo.WCF.IService1"> 
    2. < endpoint address="ws" binding="wsHttpBinding" 
      contract="Demo.WCF.IService1"> 
    3. < endpoint address="mex" binding="basicHttpBinding" 
      contract="IMetadataExchange"/>

    因为目前Silverlight只支持basicHttpBinding

    Silverlight工程需要注意的地方:

    注意其address访问地址

    1. < client> 
    2. < endpoint address="http://localhost:4584/Service1.svc" 
      binding="basicHttpBinding" 
    3. bindingConfiguration="BasicHttpBinding_IService1" 
      contract="ServiceReference1.IService1" 
    4. name="BasicHttpBinding_IService1" /> 
    5. < /client> 

    实现Silverlight调用WCF服务代码如下:

    1. view plaincopy to clipboardprint?  
    2. private void Button_Click(object sender, RoutedEventArgs e)   
    3. {   
    4. ServiceReference1.Service1Client client = new Demo.Slapp.
      ServiceReference1.Service1Client();   
    5. client.GetDataAsync(9);   
    6. client.GetDataCompleted += new EventHandler< Demo.Slapp.
      ServiceReference1.GetDataCompletedEventArgs
      >
      (client_GetDataCompleted);   
    7. client.CloseCompleted += new EventHandler< System.ComponentModel.
      AsyncCompletedEventArgs
      >
      (client_CloseCompleted);   
    8. }   
    9. void client_GetDataCompleted(object sender, Demo.Slapp.
      ServiceReference1.GetDataCompletedEventArgs e)   
    10. {   
    11. if (e.Error == null)   
    12. {   
    13. this.btnDemo.Content = e.Result;   
    14. }   
    15. else   
    16. {   
    17. this.btnDemo.Content = "eror";   
    18. }   
  • 相关阅读:
    suse12安装详解
    Centos7上部署openstack mitaka配置详解(将疑难点都进行划分)
    菜鸟帮你跳过openstack配置过程中的坑[文末新添加福利]
    openstack中dashboard页面RuntimeError: Unable to create a new session key. It is likely that the cache is unavailable.
    Multiple network matches found for name 'selfservice', use an ID to be more specific.报错
    查看 SELinux状态及关闭SELinux
    SELinux深入理解
    IP地址、子网掩码、网络号、主机号、网络地址、主机地址
    Oracle job procedure 存储过程定时任务
    POI文件导出至EXCEL,并弹出下载框
  • 原文地址:https://www.cnblogs.com/vawe86/p/2059862.html
Copyright © 2011-2022 走看看