Silverlight調(diào)用WCF服務(wù)相關(guān)應(yīng)用細(xì)節(jié)解析
許多剛剛學(xué)習(xí)了WCF的同學(xué)們肯定在實(shí)際的應(yīng)用開發(fā)中都被Silverlight調(diào)用WCF服務(wù)的相關(guān)操作折磨了一陣子。要想很好的掌握這一應(yīng)用技巧,還是需要我們從不斷的操作中去積累經(jīng)驗(yàn)。在這里我們將會為大家詳細(xì)介紹一下這方面的知識。
WCF工程中需要注意的地方:
1.新建一個crossdomain.xml文件,內(nèi)容如下
- < ?xml version="1.0" encoding="utf-8" ?>
 - < cross-domain-policy>
 - < allow-access-from domain="*" />
 - < !-- 意為:允許來自任意域名對本web服務(wù)站點(diǎn)的任意跨域訪問,
 
如要限制跨域訪問站點(diǎn):可將"*"更改為相應(yīng)域名,多個域名則為多個
< allow-access-from ... />節(jié)點(diǎn) -->- < /cross-domain-policy>
 
2.修改web.config文件內(nèi)容
- < endpoint address="" binding="basicHttpBinding"
 
contract="Demo.WCF.IService1">- < endpoint address="mex" binding="basicHttpBinding"
 
contract="IMetadataExchange"/>
因?yàn)槟壳癝ilverlight只支持basicHttpBinding
Silverlight工程需要注意的地方:
注意其address訪問地址
- < client>
 - < endpoint address="http://localhost:4584/Service1.svc"
 
binding="basicHttpBinding"- bindingConfiguration="BasicHttpBinding_IService1"
 
contract="ServiceReference1.IService1"- name="BasicHttpBinding_IService1" />
 - < /client>
 
實(shí)現(xiàn)Silverlight調(diào)用WCF服務(wù)代碼如下:
- 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";
 - }
 - }
 
以上就是我們?yōu)榇蠹医榻B的Silverlight調(diào)用WCF服務(wù)相關(guān)方法。
【編輯推薦】















 
 
 
 
 
 
 