无法在Windows Phone 8上将EAP模式应用于WCF服务客户端

时间:2013-07-25 12:18:19

标签: wcf windows-phone-8

我正在尝试应用EAP模式来调用WCF服务。相同的代码在Windows控制台应用程序上顺利运行,但在Windows Phone应用程序的Task.Wait上无关紧要地挂起。

对此问题的任何想法都将受到高度赞赏。

提前致谢

这是扩展方法和辅助函数:

public static class ServiceExtensions
{
    public static Task<string> DoGlobalizationResourcesInquiryAsyncTask(
        this Service1Client serviceClient,
        int request)
    {
        var tcs = new TaskCompletionSource<string>();
        EventHandler<GetDataCompletedEventArgs> handler = null;
        handler = (sender, e) => EAPHelpers.TransferCompletion(
            tcs,
            e,
            () => e.Result,
            () => serviceClient.GetDataCompleted -= handler);

        serviceClient.GetDataCompleted += handler;
        try
        {
            serviceClient.GetDataAsync(request, tcs);
        }
        catch
        {
            serviceClient.GetDataCompleted -= handler;
            tcs.TrySetCanceled();
            throw;
        }

        return tcs.Task;

    }
}

public static class EAPHelpers
{
    public static void TransferCompletion<T>(
        TaskCompletionSource<T> tcs,
        AsyncCompletedEventArgs e,
        Func<T> getResult,
        Action unregisterHandler)
    {
        if (e.UserState == tcs)
        {
            if (e.Cancelled) tcs.TrySetCanceled();
            else if (e.Error != null) tcs.TrySetException(e.Error);
            else tcs.TrySetResult(getResult());
            if (unregisterHandler != null) unregisterHandler();
        }
    }
}

我按如下方式调用扩展方法:

        Service1Client client = new Service1Client();
        var task = client.DoGlobalizationResourcesInquiryAsyncTask(3);
        task.Wait();
        var response = task.Result;

0 个答案:

没有答案