【问题标题】:Inconsistent ServiceStack exception handling不一致的ServiceStack异常处理
【发布时间】:2012-08-15 06:17:00
【问题描述】:

我有一个使用 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。

提前致谢。

【问题讨论】:

  • 抱歉过早发布。更新以包含问题

标签: servicestack


【解决方案1】:

您得到的 HTML 错误页面看起来不像来自 ServiceStack,请检查您的网站是否存在可能利用自己的页面劫持错误的内容,例如:&lt;customErrors /&gt;

SOAP 端点的正确行为是抛出一个 SOAP 错误,如果您使用 Soap11ServiceClientSoap12ServiceClientgeneric service clients 将被转换为 WebServiceException,如 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));
}

【讨论】:

  • 即使使用 我也会得到相同的结果。将尝试与不同的客户。
猜你喜欢
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-22
相关资源
最近更新 更多