我正在使用ServiceStack来构建Web服务API的原型,并且在测试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.....
}
}
非常基本,就像ServiceStack.net
上的hello world示例一样违规客户端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.
}
有趣的是,一个接一个地测试async和non-async也是如此:
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.
}
在所有情况下,我都可以通过Fiddler看到实际的数据是通过HTTP传输的,所以我想我对缺少api的工作方式有一些基本的了解。
欢迎任何帮助。感谢。
答案 0 :(得分:1)
阻塞同步API在响应完成之前不会返回,因为Async API是非阻塞的,因此执行会立即继续。只有在返回并处理响应时才会触发回调。
在AsyncRestClientTests.cs测试中,在声明及时返回响应之前,它使用Thread.Sleep(1000)
休眠1秒。
您还等待多久才能确定是否触发了回调?