许多刚刚学习了WCF的同学们肯定在实际的应用开发中都被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>
实现Silverlight调用WCF服务代码如下:
- view plaincopy to clipboardprint?
- 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";
- }
- }
以上就是我们为大家介绍的Silverlight调用WCF服务相关方法。
【编辑推荐】