开发者

Invoke operation 'myMethod' failed in RIA Services in Silverlight

I have a server-side WCF RIA Service that is i开发者_StackOverflow社区ntentionally throwing an exception because the user entered an invalid value. This exception comes across the wire, however, I can't figure out how to catch it. I currently have the following code:

try
{
  DomainContext.CalculateRequest(OnCalculateCompleted, null);
}
catch (Exception ex)
{
  MessageBox.Show("here");
}

...

private void OnCalculateCompleted(InvokeOperation response)
{
  try
  {
    if (response.HasError == false)
    {
      // Do stuff with result
    }
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}

How do I handle exceptions thrown by a server-side operation on the client side? None of my catch statements are being triggered. Thank you!


On the client side the InvokeOperation.HasErrors will be true and you can get the Exception object from the InvokeOperation.Error. Note, if you handled the error you should also call MarkErrorAsHandled().

Your OnCalculateCompleted might looks something like this.

private void OnCalculateCompleted(InvokeOperation response)
{
  if (response.HasError == false)
  {
    // Do stuff with result
  }
  else
  {
    MessageBox.Show(response.Error.Message);
    response.MarkErrorAsHandled();
  }
}


Yes, because in the callback (OnCalculateCompleted), exception will not be marshalled. The exception will reside in the response.Error property.

But take care, because your server-side thrown exception will NOT be found in the response.Error!

You should override your DomainService's OnError method, package your server-side exception via errorcodes or something, and on the client (SL) side, you have to unpack it again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜