调用操作“myMethod” Silverlight 中的 RIA 服务失败

发布于 2024-11-03 16:37:49 字数 506 浏览 7 评论 0原文

我有一个服务器端 WCF RIA 服务,它故意抛出异常,因为用户输入了无效值。这个异常是通过网络出现的,但是,我不知道如何捕获它。我目前有以下代码:

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);
  }
}

如何在客户端处理服务器端操作抛出的异常?我的 catch 语句都没有被触发。谢谢你!

I have a server-side WCF RIA Service that is intentionally 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

執念 2024-11-10 16:37:50

在客户端,InvokeOperation.HasErrors 将为 true,您可以从 InvokeOperation.Error 获取 Exception 对象。请注意,如果您处理了错误,您还应该调用 MarkErrorAsHandled()

您的 OnCalculateCompleted 可能看起来像这样。

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

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();
  }
}
用心笑 2024-11-10 16:37:50

是的,因为在回调(OnCalculateCompleted)中,异常不会被编组。异常将驻留在response.Error 属性中。

但要小心,因为你的服务器端抛出的异常不会在响应中找到。错误!

您应该重写 DomainService 的 OnError 方法,通过错误代码或其他内容打包服务器端异常,并且在客户端(SL)端,您必须再次解包它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文