抛出HttpErrors

时间:2019-05-02 12:59:57

标签: error-handling servicestack

ServiceStack不会在WebServiceException的responseDTO属性中填充原始响应。

我正在运行下面的代码,该代码应始终返回404响应代码,其TestResponse的ResponseStatus属性填充为“ Some bad request”,但似乎也应返回原始的良好响应,并从其中填充其output属性请求的输入属性。但是,当我查看WebServiceException responseDTO

时,我得到的是null
        public TestResponse Post(Test request)
        {
            var response = new TestResponse() { Output = request.Input };

            throw new HttpError(response, (int)HttpStatusCode.BadRequest, "Some bad request");
        }

        public TestResponse Get(Test request)
        {
            try
            {
                using (var client = new JsonServiceClient("http://localhost:5000"))
                {
                    var response =  client.Post(request);
                    return response;
                }
            }
            catch (WebServiceException ex)
            {

                throw;
            }
        }

通常,我期望WebServiceException中的responseDTO属性将包含端点的DTO,只要抛出HttpError时传递了它就可以了,但事实并非如此。我在responseDTO中仅看到默认值和每个属性的null。

1 个答案:

答案 0 :(得分:2)

当引发异常时,仅保留ResponseStatus,您可以将任何其他信息添加到其Meta字典中。

或者,您可以return a failed HTTP Response

public TestResponse Post(Test request)
{
    var response = new TestResponse() { Output = request.Input };

    base.Response.StatusCode = (int)HttpStatusCode.BadRequest;
    return response;
}