我有一个WCF服务,它引发一个异常,我试图在我的silverlight客户端代码中不恰当地捕获异常。我使用的未申报的故障进行调试的目的,这是我服务的方法:silverlight客户端不明白faultexception
[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 FaultException)
我仍然得到System.Net.WebException {远程服务器返回错误:NOTFOUND}
这些都是配置项
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
这是服务类的声明
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MySilverlightWCFService
{
....
这项服务是在同一个Silverlight的解决方案中的另一个项目。 为什么我的silverlight客户端无法获得我抛出的错误异常?
感谢您的时间...
所以Silverlight的设计没有考虑服务错误处理? :O – user20358 2011-01-30 11:13:27
看起来状态代码必须从500更改为Silverlight可以在服务端读取的内容。那么,如果它是我无法控制的服务呢?我认为这是一个蹩脚的解决方法。要再读一遍,看看我是否错过了一些东西! – user20358 2011-01-30 11:15:59