2011-06-07 79 views
0

我想等到異步WCF服務操作完成。怎麼做 ?WCF異步方法:等到操作完成

Service.WebService.GetUserCompleted += new EventHandler<BGWebService.GetUserCompletedEventArgs>(WebService_GetUserCompleted); 
Service.WebService.GetUserAsync(UserId); 

回答

0

GetUserAsync應返回IAsyncResultsee MSDN documentation)的生成的方法。您可以使用它來阻止,直到操作完成:

IAsyncResult asyncResult = Service.WebService.GetUserAsync(UserId); 
asyncResult.AsyncWaitHandle.WaitOne(); 
+0

我已經嘗試過了,但是我無法將GetUserAsync從無效類型轉換爲IAsyncResult – Tony 2011-06-07 18:52:35

+1

您使用的是爲Silverlight生成的代理嗎? – 2011-06-07 19:14:23

0

那麼我試着以不同的方式做到這一點。我沒有等待服務響應,而是打電話給我正在處理的界面。

public class Person : INotifyPropertyChanged 
{ 
    //Calling the service to get the object needed 
    private string personname 
     public string PersonName 
     { 
      get { 
       return personname; 
      } 
      set { 
       personname = value; 
       NotifyPropertyChanged("PersonName"); 
      } 
     } 

     private void NotifyPropertyChanged(string property) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(property)); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public void LoadMembersName() 
     { 
      ServiceSoapClient serviceClient = new ServiceSoapClient() {}; 
      serviceClient.GetMembersNameCompleted += (sender, e)=>{ 
      PersonName = e.Result 
     } ; 
    } 
} 

現在在界面上我們只需要加載人員的姓名並在屬性更改事件觸發後執行操作。

void Page_Load (object sender, EventArgs e) 
{ 
    Person per = new Person(); 
    per.PropertyChanged += (sender,e) => 
    { 
     //Your code here 
     Response.Write(per.PersonName); 
    }; 
    per.LoadMembersName(); 
} 

我真的很抱歉。我也從某個源複製了代碼,但是我錯誤地鏈接了該鏈接。