silverlight客户端不理解faultException
我有一个 WCF 服务抛出一个异常,我试图在我的 silverlight 客户端代码中捕获该异常,但未成功。我正在使用未声明的错误进行调试,这是我的服务方法:
[OperationContract]
public ServiceResponse MyWCFServiceMethod()
{
ServiceResponse resp = new ServiceResponse ();
//code for setting resp...
//purposely throw error here.
throw new FaultException(new FaultReason(new FaultReasonText("My fault Reason!")),new FaultCode("my fault code here"));
return resp;
}
现在在我的 silverlight 客户端视图模型中,在服务的回调方法中,我尝试像这样处理它:
private void MyServiceCallback(MyWCFServiceMethodCompletedEventArgs e)
{
if (e.Error == null)
{
//proceed normally
}
else if (e.Error is FaultException)
{
FaultException<ExceptionDetail> fault = e.Error as FaultException<ExceptionDetail>;
MessageBox.Show(fault.Detail.Message);
MessageBox.Show(fault.Reason.ToString());
}
}
在这一行 else if (e.Error is我仍然得到 System.Net.WebException {远程服务器返回错误:NotFound。}
这些是配置条目
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
这是服务类声明
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MySilverlightWCFService
{
....
该服务位于同一 silverlight 解决方案中的另一个项目中。 为什么我的 silverlight 客户端无法获取我抛出的错误异常?
感谢您抽出时间...
I have a WCF service that throws an exception which I am trying to catch unsucessfully in my silverlight client code. I am using Undeclared Faults for Debugging purposes and this is my service method :
[OperationContract]
public ServiceResponse MyWCFServiceMethod()
{
ServiceResponse resp = new ServiceResponse ();
//code for setting resp...
//purposely throw error here.
throw new FaultException(new FaultReason(new FaultReasonText("My fault Reason!")),new FaultCode("my fault code here"));
return resp;
}
Now in my silverlight client view model, in the service's callback method, I try to handle it like this:
private void MyServiceCallback(MyWCFServiceMethodCompletedEventArgs e)
{
if (e.Error == null)
{
//proceed normally
}
else if (e.Error is FaultException)
{
FaultException<ExceptionDetail> fault = e.Error as FaultException<ExceptionDetail>;
MessageBox.Show(fault.Detail.Message);
MessageBox.Show(fault.Reason.ToString());
}
}
at this line else if (e.Error is FaultException)
I still get System.Net.WebException {The remote server returned an error: NotFound.}
These are the config entries
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
This is the service class declaration
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MySilverlightWCFService
{
....
This service is in another project within the same silverlight solution.
Why is my silverlight client not able to get the fault exception I am throwing?
Thanks for your time...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,最后似乎完成这项工作的方法是在抛出故障异常之前将一行代码添加到服务中!
然后抛出实际的异常:
然后在 silverlight 中将服务回调错误处理部分从我在上面的问题中放入的内容修改为:
这对我有用。丑陋的方式!
当我有时间时,我会尝试声明错误。
ok so finally what seems to be the way to make this work is to get one line of code added to the service just before you throw the fault exception!
Then throw the actual exception:
Then in silverlight modify the service call back error handling section from what I put in my question above to:
That worked for me. Ugly way!
I will try with Declared faults when I get the time.
查看 SilverlightFaultBehavior。它将为您处理更改状态代码。
Check out the SilverlightFaultBehavior. It will handle changing the status code for you.
服务器可能会抛出 Silverlight 忽略的 HTTP 500 响应代码。您必须更改服务以返回 Silverlight 可接受的 HTTP 代码。
来自 Silverlight 3 中的数据性能和故障策略:(本文将展示您如何将 WCF 错误返回到 Silverlight。)
The server is probably throwing a HTTP 500 response code that Silverlight is ignoring. You must change the service to return a HTTP code that Silverlight will accept.
From Data Performance and Fault Strategies in Silverlight 3: (This article will show you how to return WCF faults to Silverlight.)