WCF Web 服务和FaultContract - 客户端接收安装了FaultException的SoapExc
我正在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,各位,我自己解决了。
正如我所说,一切都是对的......我只是使用了错误的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