【发布时间】:2015-02-13 20:35:41
【问题描述】:
我的 WCF 服务中有两种方法:
public interface IPriceService
{
[OperationContract(Name = "GetPrice")]
decimal GetPrice(long ID);
[OperationContract(Name = "GetProductID")]
long GetProductID(string productName);
}
在客户方面,我想做两件事。我想调用一个方法GetProductID,然后调用一个方法GetPrice。所以我这样做:
PriceService.PriceServiceClient service;
service = new PriceService.PriceServiceClient();
service.PobierzPrzejsciaCompleted += service_GetPriceCompleted;
service.PobierzStacjeBenzynoweCompleted += service_GetProductIDCompleted;
service.GetProductIDAsync(productName);
service.GetPriceAsync(Id);
GetProductID 返回一个 ID,所以我在 GetProductIDCompleted 事件中得到它。然后我将此 ID 作为参数传递给GetPrice 方法。
我想要的是:GetProductIdAsync 方法应该首先执行。 GetPriceAsync 方法应该等到前一个方法完成。
【问题讨论】:
-
他们是
awaitable吗?为什么不await service.GetProductIDAsync(productName);? -
实现TaskCompletionSource。也许this answer 会有所帮助。
-
在添加服务引用时,Windows Phone 8.1 中没有可用的“生成基于任务的操作”。
-
构建一个您定义TCS的Task,然后订阅您的服务事件,在TCS的完整委托集结果中,返回TCS.Task。然后等待你的新任务。这是converting EAP to TAP - example。
-
哦,谢谢。但这对我来说真的很难理解......
标签: c# wcf silverlight asynchronous windows-phone-8.1