0

新しい async キーワードを使用して Silverlight で Web サービスを呼び出す例を探しています。

これは私が変換しようとしているコードです:

var client = new DashboardServicesClient("BasicHttpBinding_IDashboardServices", App.DashboardServicesAddress);
client.SelectActiveDealsCompleted += (s, e) => m_Parent.BeginInvoke(() => RefreshDealsGridComplete(e.Result, e.Error));
client.SelectActiveDealsAsync();
4

2 に答える 2

1

いつでも自分でできます:

static class DashboardServicesClientExtensions
{
    //typeof(TypeIDontKnow) == e.Result.GetType()
    public static Task<TypeIDontKnow>> SelectActiveDealsTaskAsync()
    {
        var tcs = new TaskCompletionSource<TypeIDontKnow>();

        client.SelectActiveDealsCompleted += (s, e) => m_Parent.BeginInvoke(() =>
        {
            if (e.Erorr != null)
                tcs.TrySetException(e.Error);
            else
                tcs.TrySetResult(e.Result);
        };
        client.SelectActiveDealsAsync();

        return tcs.Task;
    }
};

// calling code
// you might change the return type to Tuple if you want :/
try
{
    // equivalent of e.Result
    RefreshDealsGridComplete(await client.SelectActiveDealsTaskAsync(), null);
}
catch (Exception e)
{
    RefreshDealsGridComplete(null, e);
}
于 2012-11-06T21:08:41.733 に答える
0

ターゲット バージョン .Net 4.5 でサービス コードを再生成する必要があります。async次に、キーワードを使用できます。次のようなコードを記述します。

var client = new DashboardServicesClient("BasicHttpBinding_IDashboardServices", App.DashboardServicesAddress);
// Wait here until you get the result ...
var result = await client.SelectActiveDealsAsync();
// ... then refresh the ui synchronously.
RefreshDealsGridComplete(result);

または、ContinueWithMethodeを使用します。

var client = new DashboardServicesClient("BasicHttpBinding_IDashboardServices", App.DashboardServicesAddress);
// Start the call ...
var resultAwaiter = client.SelectActiveDealsAsync();
// ... and define, what to do after finishing (while the call is running) ...
resultAwaiter.ContinueWith( async task => RefreshDealsGridComplete(await resultAwaiter));
// ... and forget it
于 2012-05-02T11:49:09.493 に答える