Silverlight調用WCF服務相關應用細節解析
作者:佚名
Silverlight調用WCF服務在實際應用中有很多比較重要的細節需要我們在運用中去注意,在這里我們就為大家詳細介紹這方面的內容。
許多剛剛學習了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服務相關方法。
【編輯推薦】
責任編輯:曹凱
來源:
CSDN