silverlight客户端不理解faultException

发布于 2024-10-15 08:00:13 字数 1453 浏览 7 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

韶华倾负 2024-10-22 08:00:13

好吧,最后似乎完成这项工作的方法是在抛出故障异常之前将一行代码添加到服务中!

System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;

然后抛出实际的异常:

throw new FaultException(new FaultReason(new FaultReasonText("My fault Reason!")),new FaultCode("my fault code here"));

然后在 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());
   }

这对我有用。丑陋的方式!

当我有时间时,我会尝试声明错误。

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!

System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;

Then throw the actual exception:

throw new FaultException(new FaultReason(new FaultReasonText("My fault Reason!")),new FaultCode("my fault code here"));

Then in silverlight modify the service call back error handling section from what I put in my question above to:

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

That worked for me. Ugly way!

I will try with Declared faults when I get the time.

So尛奶瓶 2024-10-22 08:00:13

查看 SilverlightFaultBehavior。它将为您处理更改状态代码。

Check out the SilverlightFaultBehavior. It will handle changing the status code for you.

辞别 2024-10-22 08:00:13

服务器可能会抛出 Silverlight 忽略的 HTTP 500 响应代码。您必须更改服务以返回 Silverlight 可接受的 HTTP 代码。

来自 Silverlight 3 中的数据性能和故障策略:(本文将展示您如何将 WCF 错误返回到 Silverlight。)

臭名昭著的未找到错误:
当异常发生时,HTTP
地位
500 的代码返回到
银光。浏览器网络
堆栈阻止 Silverlight
读取带有状态代码的响应
500,因此任何 SOAP 错误信息
其中包含的内容不可用于
Silverlight 客户端应用程序。甚至
如果可以检索消息,
Silverlight 2 不具备
将故障转换回
托管异常。这两个
问题已在
银光3。

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.)

Infamous NotFound Error:
When the exception is raised, an HTTP
status
code of 500 is returned to
Silverlight. The browser networking
stack prevents Silverlight from
reading responses with a status code
of 500, so any SOAP fault information
contained within is unavailable to the
Silverlight client application. Even
if the message could be retrieved,
Silverlight 2 is not capable of
converting the fault back into a
managed exception. Both of these
issues have been addressed in
Silverlight 3.

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