![](/img/trans.png)
[英]How to call asynchronously WCF Service method with WebInvokeAttribute?
[英]How to call wcf service Asynchronously
如果wcf服務設計如下,請指導我如何從客戶端Asynchronously
調用Add()
函數。 謝謝
[ServiceContract]
public interface IAddTwoNumbers
{
// If the asynchronous method pair
// appears on the client channel, the client can call
// them asynchronously to prevent blocking.
[OperationContract (AsyncPattern=true)]
IAsyncResult BeginAdd(int a, int b, AsyncCallback cb, AsyncState s);
[OperationContract]
int EndAdd(IAsyncResult r);
// This is a synchronous version of the BeginAdd/EndAdd pair.
// It appears in the client channel code by default.
[OperationContract]
int Add(int a, int b);
}
我認為最好的方法是使用Task.Factory.FromAsync
將APM模式轉換為任務模式 :
public static class WcfExt
{
public static Task<int> AddAsync(this IAddTwoNumbers service, int a, int b)
{
return Task.Factory.FromAsync(
(asyncCallback, asyncState) =>
service.BeginAdd(a, b, asyncCallback, asyncState),
(asyncResult) =>
service.EndAdd(asyncResult), null);
}
}
用法:
IAddTwoNumbers service = CreateWcfClientProxy();
int result = await service.AddAsync(a, b);
沒有異步有線通信模式,WFC也不例外。 越網絡線路的異步需要您服務,您的合同根本的變化。 具體而言,您的合同必須是雙向合同,並且必須將“結束”從服務推送到客戶端以及結果。 換句話說,通過線路,'async'成為回調。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.