2012-12-27 108 views
2

我使用ServiceStack爲Web服務API進行原型設計,並在測試GetAsync時遇到了問題。具體來說,當我期望它時,onSuccess動作不會被調用。ServiceStack GetAsync OnSuccess未解僱

這裏是我的代碼:

服務器:

[Route("/accounts/", "GET") 
public class AccountRequest : IReturn<AccountResponse> 
{ 
    public string EmailAddress {get; set;} 
} 

public class AccountResponse 
{ 
    public Account Account {get; set;} 
} 

public class AccountService : Service 
{ 
    public object Get(AccountRequest request) 
    { 
     return new AccountResponse{Account = new Account..... 
    } 
} 

非常基本的,幾乎爲每個hello上ServiceStack.net

世界的例子和有問題的客戶端GetAsync電話:

using(var client = new JsonServiceClient("some url") 
{ 
    client.GetAsync(new AccountRequest{EmailAddress = "gibbons"}, 
      response => Console.WriteLine(response.Account.Something), //This never happens 
      (response, ex) => {throw ex;}); // if it matters, neither does this 

} 

但是,這與預期完全一樣...

using(var client = new JsonServiceClient("some url") 
{ 
    var acc = client.Get(new AccountRequest{EmailAddress = "gibbons"}); 

    //acc is exactly as expected. 
} 

有趣的是,測試異步與非異步一個其他作品太后:

using(var client = new JsonServiceClient("some url") 
{ 
    client.GetAsync(new AccountRequest{EmailAddress = "gibbons"}, 
        response => Console.WriteLine(response.Account.Something), //Works 
        (response, ex) => {throw ex;}); 

    var acc = client.Get(new AccountRequest{EmailAddress = "gibbons"}); 

    //Again, acc is exactly as expected. 
} 

在所有情況下,我可以看到實際的數據經由小提琴手調過來的HTTP,所以我覺得我錯過了一些關於異步api如何工作的基本理解。

任何幫助最受歡迎。謝謝。

回答

1

阻塞同步API不會返回,直到響應完成,因爲異步API是非阻塞的,因此執行會立即執行。回調僅在返回並處理響應時觸發。

AsyncRestClientTests.cs測試中,它在睡覺之前用Thread.Sleep(1000)休眠1秒,然後聲明響應已及時返回。

您等待多久才能確定回調是否被解僱?

+0

是的 - 我只是沒有等待足夠長的迴應。謝謝。 –