C# 实施';等待&x27;将行为键入不可等待的任务

C# 实施';等待&x27;将行为键入不可等待的任务,c#,wcf,events,asynchronous,delegates,C#,Wcf,Events,Asynchronous,Delegates,访问WCF服务时,Windows Phone 8和标准.NET的API存在差异。WP8的API强制使用回调,而不是提供可等待的接口。此外,我为服务调用使用自定义头,这些服务调用还包括一些在这两个平台上不同的神怪(不过我更希望我的代码在这两个平台上都可以运行) 总之,我最终得到了一个类似的模式: private bool mOperationCompleted = false; private OperationResultType mOperationResult; p

访问WCF服务时,Windows Phone 8和标准.NET的API存在差异。WP8的API强制使用回调,而不是提供可等待的接口。此外,我为服务调用使用自定义头,这些服务调用还包括一些在这两个平台上不同的神怪(不过我更希望我的代码在这两个平台上都可以运行)

总之,我最终得到了一个类似的模式:

    private bool mOperationCompleted = false;
    private OperationResultType mOperationResult;

    public async Task<OperationResultType> WCFServiceRequestOperationName()
    {
        mClient.WCFServiceRequestOperationNameCompleted += WCFServiceRequestOperationNameCompleted;

        PerformRequest(mClient, () => mStoreClient.WCFServiceRequestOperationName(dem parameters for this particular call));

        while (!mOperationCompleted ) { await Task.Delay(500); /* delay isn't mandatory here */ }

        // Reset
        mOperationCompleted = false;

        // Return
        return mOperationResult;
    }

    void Client_WCFServiceRequestOperationNameCompleted(object sender, WCFServiceRequestOperationNameCompletedEventArgs e)
    {
        mOperationResult = e.Result;
        mOperationCompleted = true;
    }
所以我的问题是,如何将这个模式包装在一个类中,这样我就可以简单地以通用方式为任何WCF服务调用调用它。我相当肯定泛型和委托可能有解决方案,但我不能将事件作为参数传递,因此我不知道如何以泛型方式添加处理程序


我想要这样的东西,因为我不想复制粘贴并为我提出的每个请求调整它。

您不想在WCF调用中使用轮询模式。相反,请遵循以下步骤。我喜欢让它们成为扩展方法,因此它们只编写一次,可以在任何地方使用:

public static Task<OperationResult> OperationTaskAsync(this WCFService client)
{
  var tcs = new TaskCompletionSource<OperationResult>();
  OperationCompletedEventHandler handler = null;
  handler = (_, e) =>
  {
    client.OperationCompleted -= handler;
    if (e.Error != null)
      tcs.TrySetException(e.Error);
    else if (e.Cancelled)
      tcs.TrySetCanceled();
    else
      tcs.TrySetResult(e.Result);
  };
  client.OperationCompleted += handler;
  client.OperationAsync();
  return tcs.Task;
}
公共静态任务操作TaskAsync(此WCFService客户端)
{
var tcs=new TaskCompletionSource();
OperationCompletedEventHandler=null;
处理程序=(u,e)=>
{
client.OperationCompleted-=处理程序;
如果(例如错误!=null)
tcs.TrySetException(即错误);
否则(如已取消)
tcs.trysetconceled();
其他的
tcs.TrySetResult(e.Result);
};
client.OperationCompleted+=处理程序;
client.OperationAsync();
返回tcs.Task;
}

您不想在WCF呼叫中使用轮询模式。相反,请遵循以下步骤。我喜欢让它们成为扩展方法,因此它们只编写一次,可以在任何地方使用:

public static Task<OperationResult> OperationTaskAsync(this WCFService client)
{
  var tcs = new TaskCompletionSource<OperationResult>();
  OperationCompletedEventHandler handler = null;
  handler = (_, e) =>
  {
    client.OperationCompleted -= handler;
    if (e.Error != null)
      tcs.TrySetException(e.Error);
    else if (e.Cancelled)
      tcs.TrySetCanceled();
    else
      tcs.TrySetResult(e.Result);
  };
  client.OperationCompleted += handler;
  client.OperationAsync();
  return tcs.Task;
}
公共静态任务操作TaskAsync(此WCFService客户端)
{
var tcs=new TaskCompletionSource();
OperationCompletedEventHandler=null;
处理程序=(u,e)=>
{
client.OperationCompleted-=处理程序;
如果(例如错误!=null)
tcs.TrySetException(即错误);
否则(如已取消)
tcs.trysetconceled();
其他的
tcs.TrySetResult(e.Result);
};
client.OperationCompleted+=处理程序;
client.OperationAsync();
返回tcs.Task;
}