客户端调用 WCF 的几种方式
生活随笔
收集整理的這篇文章主要介紹了
客户端调用 WCF 的几种方式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)載網(wǎng)絡(luò)代碼.版權(quán)歸原作者所有.....
客戶端調(diào)用WCF的幾種常用的方式:1普通調(diào)用var factory = new DataContent.ServiceReference1.CustomerServiceClient();factory.Open();List<DataContent.ServiceReference1.Customer> lis = factory.GetAllCustomerList();factory.Close();2 異步調(diào)用 1??var?factory?=?new?DataContent.ServiceReference1.CustomerServiceClient();IAsyncResult?anynRsult?=?factory.BeginGetAllCustomerList(null,?null);List<DataContent.ServiceReference1.Customer>?lis?=?factory.EndGetAllCustomerList(anynRsult);factory.Close();
該方法將會阻塞當前線程并等待異步方法的結(jié)束,往往不能起到地多線程并發(fā)執(zhí)行應(yīng)有的作用。我們真正希望的是在異步執(zhí)行結(jié)束后自動回調(diào)設(shè)定的操作,這樣就可以采用回調(diào)的方式來實現(xiàn)這樣的機制了。
??????????2??var?factory?=?new?DataContent.ServiceReference1.CustomerServiceClient();factory.BeginGetAllCustomerList(delegate(IAsyncResult?asyncResult){lis?=?factory.EndGetAllCustomerList(asyncResult);factory.Close();},?null);
???? 3通信工廠 ??? var?factory?=?new?ChannelFactory<DataContent.ServiceReference1.ICustomerService>(new?BasicHttpBinding(),?new?EndpointAddress("http://localhost:10625/Service1.svc"));try{var?proxy?=?factory.CreateChannel();using?(proxy?as?IDisposable){return?proxy.GetAllCustomerList();}}catch?(Exception?ex){Console.WriteLine(ex.Message);return?new?List<DataContent.ServiceReference1.Customer>();}finally{factory.Close();}
4 通過事件注冊的方式進行異步服務(wù)調(diào)用 ?var?factory?=?new?DataContent.ServiceReference1.CustomerServiceClient();factory.GetAllCustomerListCompleted?+=?(sender,?e)?=>{lis?=?e.Result;};factory.GetAllCustomerListAsync();factory.Close();
客戶端調(diào)用WCF的幾種常用的方式:1普通調(diào)用var factory = new DataContent.ServiceReference1.CustomerServiceClient();factory.Open();List<DataContent.ServiceReference1.Customer> lis = factory.GetAllCustomerList();factory.Close();2 異步調(diào)用 1??var?factory?=?new?DataContent.ServiceReference1.CustomerServiceClient();IAsyncResult?anynRsult?=?factory.BeginGetAllCustomerList(null,?null);List<DataContent.ServiceReference1.Customer>?lis?=?factory.EndGetAllCustomerList(anynRsult);factory.Close();
該方法將會阻塞當前線程并等待異步方法的結(jié)束,往往不能起到地多線程并發(fā)執(zhí)行應(yīng)有的作用。我們真正希望的是在異步執(zhí)行結(jié)束后自動回調(diào)設(shè)定的操作,這樣就可以采用回調(diào)的方式來實現(xiàn)這樣的機制了。
在下面的代碼中,我們通過一個匿名方法的形式定義回調(diào)操作,由于在回調(diào)操用中輸出運算結(jié)果時需要使用到參與運算的操作數(shù),我們通過BeginGetAllCustomerList方法的最后一個object類型參數(shù)實現(xiàn)向回調(diào)操作傳遞數(shù)據(jù),在回調(diào)操作中通過IAsyncResult對象的AsyncState獲得。
??????????2??var?factory?=?new?DataContent.ServiceReference1.CustomerServiceClient();factory.BeginGetAllCustomerList(delegate(IAsyncResult?asyncResult){lis?=?factory.EndGetAllCustomerList(asyncResult);factory.Close();},?null);
???? 3通信工廠 ??? var?factory?=?new?ChannelFactory<DataContent.ServiceReference1.ICustomerService>(new?BasicHttpBinding(),?new?EndpointAddress("http://localhost:10625/Service1.svc"));try{var?proxy?=?factory.CreateChannel();using?(proxy?as?IDisposable){return?proxy.GetAllCustomerList();}}catch?(Exception?ex){Console.WriteLine(ex.Message);return?new?List<DataContent.ServiceReference1.Customer>();}finally{factory.Close();}
4 通過事件注冊的方式進行異步服務(wù)調(diào)用 ?var?factory?=?new?DataContent.ServiceReference1.CustomerServiceClient();factory.GetAllCustomerListCompleted?+=?(sender,?e)?=>{lis?=?e.Result;};factory.GetAllCustomerListAsync();factory.Close();
?
在這里做個備注:防止下次忘記
?
轉(zhuǎn)載于:https://www.cnblogs.com/w2011/p/3181946.html
總結(jié)
以上是生活随笔為你收集整理的客户端调用 WCF 的几种方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Eclipse中如何让struts.x
- 下一篇: UVA 11426 GCD-Extrem