Reputation: 9352
I am calling a SOAP service running on WildFly (JBoss) which includes this operation:
<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>
When I call this service in Fiddler with an invalid request, I receive an HTTP 500 response that looks essentially like this (I have redacted slightly):
<?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>
When I call this service with the same invalid request, using a .NET WCF service proxy generated by Visual Studio 2015, I am not getting a .NET exception and the response is null. I am unable to parse the error response.
The proxy defines the operation this way:
// 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);
How can I call the service in .NET so that I can received parsed error responses instead of getting null responses? I would like to be able to use the .NET WCF generated proxy, and avoid having to parse the raw XML response!
Upvotes: 0
Views: 868
Reputation: 776
I write my code as follows and could catch the customized exception.
MyServiceContract.
[ServiceContract]
public interface Test
{
[OperationContract]
[FaultContract(typeof(CalculatorFault))]
[XmlSerializerFormat(SupportFaults = true)]
double add(double a, double b);
}
My exception model.
[DataContract]
public class CalculatorFault
{
[DataMember]
public string OperationName { get; set; }
[DataMember]
public string Fault { get; set; }
}
My service.
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;
}
}
My client code.
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)
{
}
}
}
Upvotes: 0