我正在调用在WildFly(JBoss)上运行的SOAP服务,其中包括以下操作:
<wsdl:operation name="checkIn">
<wsdl:documentation>blah</wsdl:documentation>
<wsdl:input message="tns:checkInRequestMsg" />
<wsdl:output message="tns:checkInResponseMsg" />
<wsdl:fault name="error" message="tns:errorMsg" />
</wsdl:operation>
当我在Fiddler中使用无效请求调用此服务时,收到一个HTTP 500响应,该响应看起来基本上是这样的(我做了一些修改):
<?xml version="1.0" encoding="utf-16"?>
<SOAP:Envelope xmlns:tns="..." xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<tns:error version="1.0">
<reason>...</reason>
</tns:error>
</SOAP:Body>
</SOAP:Envelope>
当我使用由Visual Studio 2015生成的.NET WCF服务代理使用相同的无效请求调用此服务时,我没有收到.NET异常,并且响应为null。我无法解析错误响应。
代理通过以下方式定义操作:
// CODEGEN: Generating message contract since the operation checkIn is neither RPC nor document wrapped.
[System.ServiceModel.OperationContractAttribute(Action="urn:checkIn", ReplyAction="*")]
[System.ServiceModel.FaultContractAttribute(typeof(ConsoleApplication2.ServiceReference1.ServiceError), Action="urn:checkIn", Name="error")]
[System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
ConsoleApplication2.ServiceReference1.checkInResponse checkIn(ConsoleApplication2.ServiceReference1.checkInRequest request);
如何在.NET中调用服务,以便可以接收已解析的错误响应,而不是获得空响应?我希望能够使用.NET WCF生成的代理,而不必解析原始的XML响应!
答案 0 :(得分:0)
我编写如下代码,并且可以捕获自定义的异常。
MyServiceContract。
[ServiceContract]
public interface Test
{
[OperationContract]
[FaultContract(typeof(CalculatorFault))]
[XmlSerializerFormat(SupportFaults = true)]
double add(double a, double b);
}
我的异常模型。
[DataContract]
public class CalculatorFault
{
[DataMember]
public string OperationName { get; set; }
[DataMember]
public string Fault { get; set; }
}
我的服务。
public class ServiceTest : Test
{
public double add(double a, double b)
{
if (b == 0)
{
throw new FaultException<CalculatorFault>(new CalculatorFault { OperationName = "divide", Fault = "hello" },"message");
}
return a + b;
}
}
我的客户代码。
static void Main(string[] args)
{
using (ChannelFactory<Test> testf = new ChannelFactory<Test>("test"))
{
Test test = testf.CreateChannel();
try
{
test.add(0, 0);
}
catch (FaultException<CalculatorFault> ex)
{
}
}
}