use*_*358 3 silverlight wcf faultexception
我有一个WCF服务,抛出一个异常,我试图在我的Silverlight客户端代码中捕获不成功.我使用Undeclared Faults进行调试,这是我的服务方法:
[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;
}
Run Code Online (Sandbox Code Playgroud)
现在在我的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());
}
}
Run Code Online (Sandbox Code Playgroud)
在这一行else if (e.Error is FaultException) 我仍然得到System.Net.WebException {远程服务器返回错误:NotFound.}
这些是配置条目
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
Run Code Online (Sandbox Code Playgroud)
这是服务类声明
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MySilverlightWCFService
{
....
Run Code Online (Sandbox Code Playgroud)
此服务位于同一Silverlight解决方案中的另一个项目中.为什么我的silverlight客户端无法解决我抛出的故障异常?
谢谢你的时间...
好吧,所以最后似乎是这样做的方法是在抛出错误异常之前将一行代码添加到服务中!
System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
Run Code Online (Sandbox Code Playgroud)
然后抛出实际的异常:
throw new FaultException(new FaultReason(new FaultReasonText("My fault Reason!")),new FaultCode("my fault code here"));
Run Code Online (Sandbox Code Playgroud)
然后在silverlight中修改服务回调错误处理部分,从我在上面的问题中添加到:
//else if (e.Error is FaultException)
else
{
//FaultException<ExceptionDetail> fault = e.Error as FaultException<ExceptionDetail>;
//MessageBox.Show(fault.Detail.Message);
FaultException fault = e.Error as FaultException;
MessageBox.Show(fault.Reason.ToString());
}
Run Code Online (Sandbox Code Playgroud)
这对我有用.丑陋的方式!
我有空的时候会尝试使用已声明的错误.
| 归档时间: |
|
| 查看次数: |
3658 次 |
| 最近记录: |