WCF回調特殊使用技巧分享
微軟公司研發的一款基于通信方面的開發框架WCF,是一款功能非常強大的開發工具。使用技巧也非常多。其中,異步調用的時候可以使用WCF回調方法來解決。#t#
我在網上收集了許多資料,現在分享一下吧。合理地利用服務的異步調用,可以有效地提高系統性能,合理分配任務的執行。
特別對于UI應用程序而言,可以提高UI的響應速度,改善用戶體驗。在我編寫的應用程序中,下載的文件如果很大,就有必要采用異步方式。對于異步調用的完成,雖然WCF提供了諸如阻塞、等待和輪詢等機制,但最好的方式還是使用回調。
也就是利用Begin方法參數中的AsyncCallback對象。這是一個委托對象,它的定義如下所示:
public delegate void AsyncCallback(IAsyncResult ar);
利用異步方式執行服務操作,使得服務在執行過程中不會阻塞主線程,當方法執行完成后,通過AsyncCallback回調對應的方法,可以通知客戶端服務執行完畢。例如:
- //Invoke it Asynchronously
- m_service.BeginTransferDocument
(m_doc,OnTransferCompleted,null);- //Do some work;
- //callback method
- void OnTransferCompleted
(IAsyncResult result)- {
- Stream stream = m_service.
EndTransferDocument(result);- result.AsyncWaitHandle.Close();
- lbMessage.Text = string.Format
("The file {0} had been transfered
sucessfully.",- m_doc.FileName);
- }
在調用BeginTransferDocument()方法之后,主線程不會被阻塞,仍然可以繼續執行其它工作。而當服務方法執行完畢之后,會自動調用WCF回調方法,執行方法中的內容。
上述實現存在一個問題,就是對于lbMessage控件的操作。由于WCF回調方法并非運行在主線程中,如果回調方法需要更新與異步調用結果相關的界面,例如本例中的lbMessage控件,則需要將回調的調用封送(Marshal)到當前主程序界面的同步上下文中。我們可以使用 SynchronizationContext以及它的SendOrPostCallback委托,對調用進行封送:
- public
- ExplorerClientForm(){
- InitializeComponent();
- m_synchronizationContext =
SynchronizationContext.Current;- }
- private SynchronizationContext
m_synchronizationContext;
則WCF回調方法修改為:
- //callback
- method void OnTransferCompleted
(IAsyncResult result)- {
- Stream stream = m_service.EndTrans
ferDocument(result);- result.AsyncWaitHandle.Close();
- SendOrPostCallback
- callback = delegate
- {
- lbMessage.Text = string.Format
("The file {0} had been transfered
sucessfully.", m_doc.FileName);- };
- m_synchronizationContext.Send
(callback,null);