How can I use async/await to call a webservice?

前端 未结 5 727
清酒与你
清酒与你 2020-12-14 07:08

I have a webservice written in Yii (php framework).

I use C# and Visual Studio 2012 to develop a WP8 application. I added a service reference to my project (Add Serv

5条回答
  •  情话喂你
    2020-12-14 07:40

    I've had to do this a couple of times over the last year and I've used both @Judah's code above and the original example he has referenced but each time I've hit on the following problem with both: the async call works but doesn't complete. If I step through it I can see that it will enter the TransferCompletion method but the e.UserState == tcs will always be false.

    It turns out that web service async methods like the OP's loginAsync have two signatures. The second accepts a userState parameter. The solution is to pass the TaskCompletionSource object you created as this parameter. This way the e.UserState == tcs will return true.

    In the OP, the e.UserState == tcs was removed to make the code work which is understandable - I was tempted too. But I believe this is there to ensure the correct event is completed.

    The full code is:

    public static Task RaiseInvoiceAsync(this Client client, string userName, string password)
    {
        var tcs = CreateSource();
        LoginCompletedEventHandler handler = null;
        handler = (sender, e) => TransferCompletion(tcs, e, () => e, () => client.LoginCompleted -= handler);
        client.LoginCompleted += handler;
    
        try
        {
            client.LoginAsync(userName, password, tcs);
        }
        catch
        {
            client.LoginCompleted -= handler;
            tcs.TrySetCanceled();
            throw;
        }
    
        return tcs.Task;
    }
    

    Alternatively, I believe there is a tcs.Task.AsyncState property too that will provide the userState. So you could do something like:

    if (e.UserState == taskCompletionSource || e.UserState == taskCompletionSource?.Task.AsyncState)
    {
        if (e.Cancelled) taskCompletionSource.TrySetCanceled();
        else if (e.Error != null) taskCompletionSource.TrySetException(e.Error);
        else taskCompletionSource.TrySetResult(getResult());
        unregisterHandler();
    }
    

    This was what I tried initially as it seemed a lighter approach and I could pass a Guid rather than the full TaskCompletionSource object. Stephen Cleary has a good write-up of the AsyncState if you're interested.

提交回复
热议问题