![WCF服務(wù)以控制臺程序為宿主時Silverlight的跨域訪問問題_第1頁](http://file4.renrendoc.com/view/2c15f6f8ccf131002fd9cc0771797f25/2c15f6f8ccf131002fd9cc0771797f251.gif)
![WCF服務(wù)以控制臺程序為宿主時Silverlight的跨域訪問問題_第2頁](http://file4.renrendoc.com/view/2c15f6f8ccf131002fd9cc0771797f25/2c15f6f8ccf131002fd9cc0771797f252.gif)
![WCF服務(wù)以控制臺程序為宿主時Silverlight的跨域訪問問題_第3頁](http://file4.renrendoc.com/view/2c15f6f8ccf131002fd9cc0771797f25/2c15f6f8ccf131002fd9cc0771797f253.gif)
![WCF服務(wù)以控制臺程序為宿主時Silverlight的跨域訪問問題_第4頁](http://file4.renrendoc.com/view/2c15f6f8ccf131002fd9cc0771797f25/2c15f6f8ccf131002fd9cc0771797f254.gif)
![WCF服務(wù)以控制臺程序為宿主時Silverlight的跨域訪問問題_第5頁](http://file4.renrendoc.com/view/2c15f6f8ccf131002fd9cc0771797f25/2c15f6f8ccf131002fd9cc0771797f255.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
WCFWCF題前幾天爭論SilverLight方法寫出來,也便利其它朋友借鑒。SilverLight時為了測試與WCF的通訊,創(chuàng)立了一個把握臺的應(yīng)用程序做為WCF效勞的宿主程序。同時創(chuàng)立了一個SilverLight應(yīng)用程序和一個承載SilverLight的Web程序.解決方案如圖:把握臺程序:添加WCF效勞命名為WCFService.cs,并添加效勞,代碼如下:namespaceWCFBase{//留意:假設(shè)更改此處的類名“WCFService“,也必需更App.config中對“WCFService“的引用。publicclassWCFService:IWCFService{publicintDoWork(inta,intb){returna+b;}publicintDoWork2(inta,intb){returna-b;}}}系統(tǒng)會自動添加App.config文件和相關(guān)配置節(jié)信息內(nèi)容如下:<?xmlversion=“1.0“encoding=“utf-8“?><configuration><system.serviceModel><serviceHostingEnvironmentaspNetCompatibilityEnabled=“true“/><behaviors><serviceBehaviors><behaviorname=“WCFBase.WCFServiceBehavior“><serviceMetadataGetEnabled=“true“/><serviceDebugincludeExceptionDetailInFaults=“false“/></behavior></serviceBehaviors></behaviors><services><servicebehaviorConfiguration=“WCFBase.WCFServiceBehavior“name=“WCFBase.WCFService“><endpointaddress=““binding=“basicBinding“contract=“WCFBase.IWCFService“><identity><dnsvalue=“l(fā)ocalhost“/></identity></endpoint><endpointaddress=“mex“binding=“mexBinding“contract=“IMetadataExchange“/><host><baseAddresses><addbaseAddress=“://localhost:9090/WCFService/“/></baseAddresses></host></service></services></system.serviceModel></configuration>WCF效勞翻開主程序入口Main方法,代碼如下namespaceWCFBase{classProgram{staticvoidMain(string[]args){ServiceHosthost=newServiceHost(typeof(WCFService));host.Open;Console.WriteLine(“I”amHere.“);Console.ReadKey;host.Close;}}}至此運行該把握臺程序,WCF效勞啟可使用。WCF可以垮域訪問在把握臺程序根目添加clientaccesspolicy.xml文件內(nèi)容如下:<?xmlversion=“1.0“encoding=“utf-8“?><access-policy><cross-domain-access><policy><allow-from-request-headers=“*“><domainuri=“*“/></allow-from><grant-to><resourcepath=“/“include-subpaths=“true“/></grant-to></policy></cross-domain-access></access-policy>添加接口IDomainService(名稱可自定義)首先要添加System.ServiceModel.Web引用代碼如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingSystem.Xml;usingSystem.ServiceModel;usingSystem.ServiceModel.Channels;namespaceWCFBase{/**////<summary>///跨域訪問策略效勞接口///留意:假設(shè)你更了ICrossDomainService接口,需要同時更App.Config///</summary>[ServiceContract]publicinterfaceIDomainService{[OperationContract][WebGet(UriTemplate=“ClientAccessPolicy.xml“)]MessageProvidePolicyFile;}}實現(xiàn)接口IDomainService類:DomainService(可自定義)代碼如下:namespaceWCFBase{publicclassDomainService:IDomainService{#regionIDomainService成員publicSystem.ServiceModel.Channels.MessageProvidePolicyFile{FileStreamfilestream=File.Open(@“ClientAccessPolicy.xml“,FileMode.Open);//EitherspecifyClientAcessPolicy.xmlfilepathproperly//orputthatin/BinfolderoftheconsoleapplicationXmlReaderreader=XmlReader.Create(filestream);System.ServiceModel.Channels.Messageresult=Message.CreateMessage(MessageVersion.None,““,reader);returnresult;}//CrossDomainServiceBehavior#endregion}}App.config文件修改后為<?xmlversion=“1.0“encoding=“utf-8“?><configuration><system.serviceModel><serviceHostingEnvironmentaspNetCompatibilityEnabled=“true“/><behaviors><serviceBehaviors><behaviorname=“WCFBase.WCFServiceBehavior“><serviceMetadataGetEnabled=“true“/><serviceDebugincludeExceptionDetailInFaults=“false“/></behavior></serviceBehaviors><endpointBehaviors><behaviorname=“DomainServiceBehavior“><web/></behavior></endpointBehaviors></behaviors><services><servicebehaviorConfiguration=“WCFBase.WCFServiceBehavior“name=“WCFBase.WCFService“><endpointaddress=““binding=“basicBinding“contract=“WCFBase.IWCFService“><identity><dnsvalue=“l(fā)ocalhost“/></identity></endpoint><endpointaddress=“mex“binding=“mexBinding“contract=“IMetadataExchange“/><host><baseAddresses><addbaseAddress=“://localhost:9090/WCFService/“/></baseAddresses></host></service><servicename=“WCFBase.DomainService“><endpointaddress=““behaviorConfiguration=“DomainServiceBehavior“binding=“webBinding“contract=“WCFBase.IDomainService“/><host><baseAddresses><addbaseAddress=“://localhost:9090/“/></baseAddresses></host></service></services></system.serviceModel></configuration>灰色區(qū)域為添加的信息,還沒有完成,連續(xù)...Main方法代碼如下:namespaceWCFBase{classProgram{staticvoidMain(string[]args){ServiceHosthost=newServiceHost(typeof(WCFService));host.Open;ServiceHostcrossDomainserviceHost=newServiceHost(typeof(DomainService));crossDomainserviceHost.Open;Console.WriteLine(“I”amHere.“);Console.ReadKey;host.Close;}}}灰色區(qū)域為添加代碼最終程序構(gòu)造如下:到此WCF效勞即可實現(xiàn)垮域訪問.留意:clientaccesspolicy.xml文件復(fù)制到WCFBase.exe一樣名目下。SilverLightWCF通訊測試運行把握臺程序WCFBase.exe如圖:在SilverLight程序中添加“效勞引用“,地址為://localhost:9090/WCFService/SilverLight代碼如下privatevoidbtnTest_Click(objectsender,RoutedEventArgse){ServiceReference1.WCFServiceClientwcf=newSilverlightApp.ServiceReference1.WCFServiceClient;wcf.DoWorkCompleted+=newEventHandler<SilverlightApp.ServiceReference1.DoWorkCompletedEventArgs>(wcf_DoWorkCompleted);wcf.DoWorkAsync(1,2);}voidwcf_D
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 農(nóng)業(yè)種植勞務(wù)合同范本
- 代工訂貨合同范例
- 加工代理合同范本
- 2025-2030年手工門牌設(shè)計企業(yè)制定與實施新質(zhì)生產(chǎn)力戰(zhàn)略研究報告
- 加工服裝服飾委托合同范本
- 會場安全協(xié)議合同范例
- 2025-2030年復(fù)合墻板生產(chǎn)線行業(yè)跨境出海戰(zhàn)略研究報告
- 2025-2030年在線宗教文化書籍行業(yè)跨境出海戰(zhàn)略研究報告
- 2025-2030年提拉米蘇奶昔企業(yè)制定與實施新質(zhì)生產(chǎn)力戰(zhàn)略研究報告
- 藥店營業(yè)員轉(zhuǎn)正申請書
- 詩詞寫作入門
- 學(xué)校教育中的STEM教育模式培訓(xùn)課件
- 《酒店客房管理課件》
- 電器整機新產(chǎn)品設(shè)計DFM檢查表范例
- 樁基礎(chǔ)工程文件歸檔內(nèi)容及順序表
- 第四單元細胞的物質(zhì)輸入和輸出(單元教學(xué)設(shè)計)高一生物(人教版2019必修1)
- 《公路路基路面現(xiàn)場測試規(guī)程》(3450-2019)
- 不同產(chǎn)地半夏總生物堿含量測定
- 2023年新疆中考數(shù)學(xué)試卷真題及答案
- 生物必修2教學(xué)進度表
- 對北京古建筑天壇的調(diào)查報告
評論
0/150
提交評論