使用Invoke调用WCF RIA服务方法时,返回类型是否会影响已完成回调的执行时间?

使用Invoke调用WCF RIA服务方法时,返回类型是否会影响已完成回调的执行时间?,wcf,silverlight,asynchronous,wcf-ria-services,ria,Wcf,Silverlight,Asynchronous,Wcf Ria Services,Ria,我继承了一个Silverlight5应用程序。在服务器端,它有一个DomainContext(服务),其方法标记为 [Invoke] public void DoIt { do stuff for 10 seconds here } 在客户端,它有一个ViewModel方法,其中包含以下内容: var q = Context.DoIt(0); var x=1; var y=2; q.Completed += (a,b) => DoMore(x,y); 我的两个问题是 1) 在我附加q

我继承了一个Silverlight5应用程序。在服务器端,它有一个DomainContext(服务),其方法标记为

[Invoke]
public void DoIt
{
 do stuff for 10 seconds here
}
在客户端,它有一个ViewModel方法,其中包含以下内容:

var q = Context.DoIt(0);
var x=1; var y=2;
q.Completed += (a,b) => DoMore(x,y);
我的两个问题是

1) 在我附加
q.Completed
时,是否已激活
DoIt
,以及

2) 退货类型(作废)是否已进入计时

现在,我知道还有另一种方法可以调用
DoIt
,即:

var q = Context.DoIt(0,myCallback);
这让我觉得调用的两种方法是互斥的。

尽管DoIt()是在远程计算机上执行的,但最好立即附加已完成的事件处理程序。否则,当进程完成时,您可能会错过回调

你说得对。调用DoIt的两种方式是相互排斥的

如果你有复杂的逻辑,你可能想考虑使用BCL异步库。看这个

使用async,您的代码将如下所示:

// Note: you will need the OperationExtensions helper
public async void CallDoItAndDosomething()
{
   this.BusyIndicator.IsBusy = true;
   await context.DoIt(0).AsTask();
   this.BusyIndicator.IsBusy = false;
}

public static class OperationExtensions
{
  public static Task<T> AsTask<T>(this T operation)
    where T : OperationBase
  {
    TaskCompletionSource<T> tcs =
      new TaskCompletionSource<T>(operation.UserState);

    operation.Completed += (sender, e) =>
    {
      if (operation.HasError && !operation.IsErrorHandled)
      {
        tcs.TrySetException(operation.Error);
        operation.MarkErrorAsHandled();
      }
      else if (operation.IsCanceled)
      {
        tcs.TrySetCanceled();
      }
      else
      {
        tcs.TrySetResult(operation);
      }
    };

    return tcs.Task;
  }
}
//注意:您将需要OperationExtensions帮助程序
公共异步void calldoi和dosomething()
{
this.BusyIndicator.IsBusy=true;
wait context.DoIt(0.AsTask();
this.BusyIndicator.IsBusy=false;
}
公共静态类操作扩展
{
公共静态任务AsTask(此T操作)
其中T:OperationBase
{
TaskCompletionSource tcs=
新TaskCompletionSource(operation.UserState);
操作。已完成+=(发送方,e)=>
{
if(operation.HasError&&!operation.IsErrorHandled)
{
tcs.TrySetException(操作错误);
操作。MarkErrorAsHandled();
}
else if(操作被取消)
{
tcs.trysetconceled();
}
其他的
{
tcs.TrySetResult(操作);
}
};
返回tcs.Task;
}
}