您的位置:首页 > 移动开发 > Objective-C

sliverlight调用wcf服务详解

2009-02-28 16:35 495 查看
silverlight中调用wcf服务好好一阵的折腾啊。。。。终于搞定了,做个记录顺便分享一下

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

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

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

2.修改web.config文件内容

  <endpoint address="" binding="basicHttpBinding" contract="Demo.Wcf.IService1">
  <endpoint address="mex" binding="basicHttpBinding" contract="IMetadataExchange"/>

因为目前silverlight只支持basicHttpBinding

silverlight工程需要注意的地方:

 

注意其address访问地址

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

 

实现代码如下:

private void Button_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client client = new Demo.Slapp.ServiceReference1.Service1Client();
client.GetDataAsync(9);
client.GetDataCompleted += new EventHandler<Demo.Slapp.ServiceReference1.GetDataCompletedEventArgs>(client_GetDataCompleted);
client.CloseCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_CloseCompleted);
}

void client_GetDataCompleted(object sender, Demo.Slapp.ServiceReference1.GetDataCompletedEventArgs e)
{
if (e.Error == null)
{
this.btnDemo.Content = e.Result;

}
else
{
this.btnDemo.Content = "eror";
}
}


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息