I have a project in which I’m using ServiceStack in 2 capacities
- Restful webservice provider to which my asp.net mvc application will connect and interact
- SOAP services which are provided to 3rd party, in these I have to conform with their specification.
My problem is that after following the instructions at SOAP support (specifically “Convert SOAP Exceptions to SOAP Faults”) I get empty responses.
So I have the following definition
public GetXPriceResponse Any(GetXPrice request)
then I validate as such
List<ErrorCode> validationErrorCodes = new List<ErrorCode>();
var result = _xBusinessService.GetXPrice(x, validationErrorCodes, logInfo);
if (validationErrorCodes.Count > 0)
{
validationErrorCodes = validationErrorCodes.Distinct().ToList();
validationErrorCodes.Sort(new ErrorCodeComparer());
}
then if I find any validation errors, I try to raise an exception
var fault = new ApiFault
{
Status = "Fail",
ErrorCode = validationErrorCodes.Select(vec => (int)vec.Id).ToArray(),
ErrorDescription = validationErrorCodes.Select(vec => vec.Description).ToArray(),
};
string reason = validationErrorCodes.Count > 0 ? "Validation Errors Encountered" : "An Error Occured";
throw new FaultException<ApiFault>(fault, reason);
I also have the following in AppHost (copy pasted from the mentioned url)
ServiceExceptionHandlers.Add((req, request, ex) =>
{
var requestMsg = req.GetItem("SoapMessage") as System.ServiceModel.Channels.Message;
if (requestMsg != null)
{
var msgVersion = requestMsg.Version;
using (var response = XmlWriter.Create(req.Response.OutputStream))
{
var message = System.ServiceModel.Channels.Message.CreateMessage(
msgVersion, new FaultCode("Receiver"), ex.ToString(), null);
message.WriteMessage(response);
}
req.Response.End();
}
return null;
});
with this setup, if I use SoapUI to query with a validation error, I get the following in the raw view
HTTP/1.1 200 OK
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
Set-Cookie: ss-id=elrMfKcu5oQ9O9Skkv75; path=/; HttpOnly
Set-Cookie: ss-pid=05WeMGTJ6LEQwQz6gXxd; expires=Thu, 03-Dec-2037 10:53:41 GMT; path=/; HttpOnly
X-SourceFiles: =?UTF-8?B?QzpcRGV2XFZpc2l0b3JJbnN1cmFuY2VcVmlzaXRvckluc3VyYW5jZUFwcGxpY2F0aW9uXEFwcFdTXGFwaVxzb2FwMTE=?=
X-Powered-By: ASP.NET
Date: Sun, 03 Dec 2017 10:53:41 GMT
and if I remove it, I get the html for the usual exception page from aps.net as the response
How can I get the fault exceptions from ServiceStack using SOAP?
note that I have no control over the 3rd party codebase, I need to comply with their standards.
my expectation is to have something like this
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/2009/WebFault">a:BadRequest</faultcode>
<faultstring xml:lang="en-US">Bad Request</faultstring>
<detail>
<CCHIFaultContract xmlns="http://schemas.datacontract.org/2004/07/CCHIVisitVisa.Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<FaultContracts>
<FaultContract>
<ErrorCode>113</ErrorCode>
<ErrorMessage>Gender must be 1 -> Male, 2 -> Female </ErrorMessage>
<Type i:nil="true"/>
</FaultContract>
<FaultContract>
<ErrorCode>102</ErrorCode>
<ErrorMessage>Email format must be xx@xx.xx </ErrorMessage>
<Type i:nil="true"/>
</FaultContract>
</FaultContracts>
</CCHIFaultContract>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>