在WCF服务中链接异步方法

时间:2012-10-27 01:25:53

标签: .net wcf asynchronous async-await

我在WCF服务中的Async模式和链接方法遇到了一些麻烦。客户端异步调用WCF,然后服务方法需要异步打开SOAP代理并调用一些方法。

问题是Soap服务的性质是顺序的,这意味着我需要一个结果,然后才能获得下一个结果并将其返回给调用的WCF服务。我正在尝试使用异步模式,但在这里失败了,因为顺序调用会阻塞其他异步调用直到它完成。

有没有办法将这些异步SOAP方法链接起来,以防止出现瓶颈?

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class DetailsService : IDetailService
{

    public DetailEntity GetDetails(DetailEntity entity)
    {

        TransactionUtilities.GetTransactionDetails(entity);

         // return modified entity with details
        return entity;

    }

}

public static class TransactionUtilities
{
    static SoapClient SoapProxy { get; set; }

    public static void GetTransactionDetails(DetailEntity entity)
    {
        IAsyncResult ar = SoapProxy.BeginExecuteTransaction(loginToken, new AsyncCallback(OnExecuteTransactionEnd), null);
        // will this block other async calls?
        ar.AsyncWaitHandle.WaitOne();
        string transactionId = SoapProxy.EndExecuteTransaction(ar);

        // another async call dependent on above transactionId
        IAsyncResult ar = SoapProxy.BeginGetTransactionResult(loginToken, processId, new AsyncCallback(OnTransactionResultEnd), null);
        ar.AsyncWaitHandle.WaitOne();
        Row[] TransactionResult = SoapProxy.EndGetTransactionResult(ar);

        // do work with result
    }

            // call back methods, just haning out doing nothing
    static void OnExecuteTransactionEnd(IAsyncResult result) { }
    static void OnTransactionResultEnd(IAsyncResult result) { }

}

0 个答案:

没有答案