调用操作“myMethod” Silverlight 中的 RIA 服务失败
我有一个服务器端 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在客户端,
InvokeOperation.HasErrors
将为true
,您可以从InvokeOperation.Error
获取 Exception 对象。请注意,如果您处理了错误,您还应该调用MarkErrorAsHandled()
。您的 OnCalculateCompleted 可能看起来像这样。
On the client side the
InvokeOperation.HasErrors
will betrue
and you can get the Exception object from theInvokeOperation.Error
. Note, if you handled the error you should also callMarkErrorAsHandled()
.Your OnCalculateCompleted might looks something like this.
是的,因为在回调(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.