1

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

提前致谢。

4

1 回答 1

1

您得到的 HTML 错误页面看起来不像来自ServiceStack,请检查您的网站是否有可能使用自己的页面劫持错误的内容,例如:<customErrors />.

SOAP 端点的正确行为是抛出 SOAP 错误,如果您使用的是通用服务客户端Soap11ServiceClientSoap12ServiceClient 通用服务客户端,则会将其转换为集成测试WebServiceException中所示的a :

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));
}
于 2012-08-14T22:17:00.227 回答