WCF Web 服务和FaultContract - 客户端接收安装了FaultException的SoapExc

发布于 2024-08-28 19:12:54 字数 2270 浏览 2 评论 0原文

我正在开发 WCF Web 服务并在 mvc2 应用程序中使用它。 我的问题是,我在带有自定义FaultDetail的方法上使用FaultContracts,并且抛出了很多faultException,但是当客户端收到异常时,它收到一个正常的SoapException,而不是我从服务端抛出的FaultException。

以下是一些代码:

自定义故障详细信息类:

[DataContract]
public class MyFaultDetails
{
[DataMember]
public string Message { get; set; }
}

对服务合同的操作:

[OperationContract]
[FaultContract(typeof(MyFaultDetails))]
void ThrowException();

实现:

public void ThrowException()
{
var details = new MyFaultDetails { Message = "Exception Test" };
throw new FaultException<MyFaultDetails >(details , new FaultReason(details .Message), new FaultCode("MyFault"));
}

客户端:

try
{
// Obv proxy init etc..
service.ThrowException();
}
catch (FaultException<MyFaultDetails> ex)
{
// stuff
}
catch (Exception ex)
{
// stuff
}

我期望的是捕获FaultException,而不是跳过该捕获并使用SoapException类型的异常进行下一个捕获。

我错过了什么吗?

我红了很多关于在 WCF 中使用错误契约的线程,我所做的似乎很好。 我查看了生成的 wsdl 和 xsd,它们看起来不错。这是有关此方法的片段:

 <wsdl:operation name="ThrowException">
      <wsdl:input wsaw:Action="http://tempuri.org/IAnyJobService/ThrowException" message="tns:IAnyJobService_ThrowException_InputMessage" />
      <wsdl:output wsaw:Action="http://tempuri.org/IAnyJobService/ThrowExceptionResponse" message="tns:IAnyJobService_ThrowException_OutputMessage" />
      <wsdl:fault wsaw:Action="http://tempuri.org/IAnyJobService/ThrowExceptionMyFaultDetailsFault" name="MyFaultDetailsFault" message="tns:IAnyJobService_ThrowException_MyFaultDetailsFault_FaultMessage" />
    </wsdl:operation>

<wsdl:operation name="ThrowException">
      <soap:operation soapAction="http://tempuri.org/IAnyJobService/ThrowException" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
      <wsdl:fault name="MyFaultDetailsFault">
        <soap:fault use="literal" name="MyFaultDetailsFault" namespace="" />
      </wsdl:fault>
    </wsdl:operation>

有帮助吗?

提前致谢

问候

亚历山德罗

i'm developing a WCF Webservice and consuming it within a mvc2 application.
My problem is that i'm using FaultContracts on my methods with a custom FaultDetail and i'm throwing manyally the faultexception but when the client receive the exception , it receives a normal SoapException instead of my FaultException that i throwed from the service side.

Here is some code:

Custom Fault Detail Class:

[DataContract]
public class MyFaultDetails
{
[DataMember]
public string Message { get; set; }
}

Operation on service contract:

[OperationContract]
[FaultContract(typeof(MyFaultDetails))]
void ThrowException();

Implementation:

public void ThrowException()
{
var details = new MyFaultDetails { Message = "Exception Test" };
throw new FaultException<MyFaultDetails >(details , new FaultReason(details .Message), new FaultCode("MyFault"));
}

Client side:

try
{
// Obv proxy init etc..
service.ThrowException();
}
catch (FaultException<MyFaultDetails> ex)
{
// stuff
}
catch (Exception ex)
{
// stuff
}

What i expect is to catch the FaultException , instead that catch is skipped and the next catch is taken with an exception of type SoapException.

Am i missing something ?

i red a lot of threads about using faultcontracts within wcf and what i did seems to be good.
I had a look at the wsdl and xsd generated and they look fine. here's a snippet regarding this method:

 <wsdl:operation name="ThrowException">
      <wsdl:input wsaw:Action="http://tempuri.org/IAnyJobService/ThrowException" message="tns:IAnyJobService_ThrowException_InputMessage" />
      <wsdl:output wsaw:Action="http://tempuri.org/IAnyJobService/ThrowExceptionResponse" message="tns:IAnyJobService_ThrowException_OutputMessage" />
      <wsdl:fault wsaw:Action="http://tempuri.org/IAnyJobService/ThrowExceptionMyFaultDetailsFault" name="MyFaultDetailsFault" message="tns:IAnyJobService_ThrowException_MyFaultDetailsFault_FaultMessage" />
    </wsdl:operation>

<wsdl:operation name="ThrowException">
      <soap:operation soapAction="http://tempuri.org/IAnyJobService/ThrowException" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
      <wsdl:fault name="MyFaultDetailsFault">
        <soap:fault use="literal" name="MyFaultDetailsFault" namespace="" />
      </wsdl:fault>
    </wsdl:operation>

Any help ?

Thanks in advance

Regards

Alessandro

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦境 2024-09-04 19:12:54

好的,各位,我自己解决了。
正如我所说,一切都是对的......我只是使用了错误的httpBinding(基本的而不是ws)。
如果你不使用 wsHttpBinding ,所有漂亮的FaultContract机制就根本不起作用。

非常感谢

问候

亚历山德罗

Ok guys, solved by myself.
As i said , all was right ... i was just using the wrong httpBinding (basic and not ws).
If u dont use the wsHttpBinding , all the beautiful FaultContract mechanism it's simply not working.

Thanks at all

Regards

Alessandro

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