ServiceStack异常处理不一致

时间:2012-08-14 21:37:26

标签: servicestack

我有一个使用ServiceStack构建的简单服务

public class GetContactMasterDataService : IService<GetContactMasterData>
{
    public object Execute(GetContactMasterData getContactMasterData)
    {            
        return ContactApi.FetchContactMasterData();
    }
}

在不同的命名空间中:

public class GetContactMasterData
{

}

public class GetContactMasterDataResponse
{
    public ResponseStatus ResponseStatus { get; set; }
}

public static GetContactMasterDataResponse FetchContactMasterData()
{
    throw new ApplicationException("CRASH");
}

当我发送JSON请求时,我正确地得到:

{
  "ResponseStatus":{
  "ErrorCode":"ApplicationException",
  "Message":"CRASH",
}
}

当我用soapUI发送soap12请求时,我得到典型的黄色死亡屏幕

<html>
<head>
    <title>CRASH</title>
...
<h2> <i>CRASH</i> </h2></span>
<b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
...
<b> Exception Details: </b>System.ApplicationException: CRASH<br><br>

这是预期的行为吗?如何获得与JSON响应类似的整齐的序列化ResponseStatus。

提前致谢。

1 个答案:

答案 0 :(得分:1)

您获得的HTML错误页面看起来并非来自ServiceStack,请检查您的网站是否有可能通过其自己的页面劫持错误的内容,例如:<customErrors />

SOAP端点的正确行为是抛出SOAP错误,如果您使用Soap11ServiceClientSoap12ServiceClient generic service clients,则会将其转换为WebServiceException as见Integration test

var client = new Soap12ServiceClient(ServiceClientBaseUri);
try
{
    var response = client.Send<AlwaysThrowsResponse>(
        new AlwaysThrows { Value = TestString });

    Assert.Fail("Should throw HTTP errors");
}
catch (WebServiceException webEx)
{
    var response = (AlwaysThrowsResponse) webEx.ResponseDto;
    var expectedError = AlwaysThrowsService.GetErrorMessage(TestString);
    Assert.That(response.ResponseStatus.ErrorCode,
        Is.EqualTo(typeof(NotImplementedException).Name));
    Assert.That(response.ResponseStatus.Message,
        Is.EqualTo(expectedError));
}