error-handling - 客户端WebServiceException的ResponseStatus为null,而没有明确的ResponseStatus

标签 error-handling client servicestack

我对ServiceStack还是很陌生,我遵循http://nilsnaegele.com/codeedge/servicestack1.html的示例,该示例一直对我有用。
我读到新API中不需要DTO响应声明中的显式StatusResponse字段,但在这里似乎没有得到预期的行为。

使用ServiceStack 3.9.71。

我在EntryService帖子中引入了一个Exception来了解客户端的处理方式。

public object Post(Entry request)
{
    if (request.Quantity == 3)
    {
        throw new WebException("post entry");
    }
}


public class EntryResponse
{
    public int Id { get; set; }
}

然后在客户端发布条目时处理异常。
    try
    {
        var entryRequest = new Entry {Quantity = quantity, EntryTime = DateTime.Now};
        var response = client.Send(entryRequest);
        Console.WriteLine("Response: {0}", response.Id);
    }
    catch (WebServiceException wse)
    {
        // At this point wse.ResponseStatus field is null.
    }

我测试了明确地将ResponseStatus字段添加到EntryResponse的过程,这在客户端上填充了ResponseStatus,而对客户端代码没有任何更改。

然后,我尝试按如下所示在StatusRequestService中引发异常,以查看第二个Web服务客户端请求是否将以相同的方式运行,并且看起来其行为有所不同。
public object Any(StatusRequest request)
{
    if (request.Lever == 3)
    {
        throw new WebException("get status.");
    }
}

随着以下。
public class StatusResponse
{
    public int Total { get; set; }
    public int Goal { get; set; }
}

然后按照
try
{
    var postResponse = client.Post<StatusResponse>("status", new StatusRequest { Date = DateTime.Now, Lever = 3 });
    Console.WriteLine("{0} of {1} achieved", postResponse.Total, postResponse.Goal);
}
catch (WebServiceException wse)
{
    // At this point wse.ResponseStatus field is valid and filled in.
}

最佳答案

如果要使用{RequestDto}Response约定并确保返回ResponseStatus,则必须选择加入并将其添加到Response DTO,例如:

public class StatusResponse
{
    public int Total { get; set; }
    public int Goal { get; set; }

    public ResponseStatus ResponseStatus { get; set; }
}

这是因为存在遵循约定{RequestDto}Response命名约定的explicit exception for Responses:

如果存在:

无论服务方法的响应类型如何,都会返回{RequestDto}Response。如果{RequestDto}Response DTO具有 ResponseStatus 属性,则将其填充,否则将不返回 ResponseStatus 。 (如果已使用{ResponseDto}Response属性修饰了[DataContract]/[DataMember]类和属性,则还需要修饰 ResponseStatus )。

否则,如果没有:

使用填充的 ResponseStatus 属性返回通用ErrorResponse

Service Clients透明地处理不同的错误响应类型,对于无模式格式(如JSON/JSV/etc),在自定义或通用ErrorResponse中返回 ResponseStatus 之间没有实际可见的区别-因为它们都在网络上输出相同的响应。

关于error-handling - 客户端WebServiceException的ResponseStatus为null,而没有明确的ResponseStatus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20533553/

相关文章:

java - 客户端和服务器之间的通信出现一些故障

php - 通过客户端上传(.sql 文件)在 PHP 中恢复 MySQL 数据库

servicestack - ServiceStack 中 DTO 中的值结构

c# - 将Web服务迁移到.NET Core 2.0并返回json

java - 检索方法应该返回 'null' 还是在无法生成返回值时抛出异常?

vba - Outlook VBA错误440 : “Array Index out of Bounds” or “Automation error” ?

c - 如何使用 C API 在 Linux 下构建独立的 MQSeries 客户端?

node.js - 处理 Express 框架中的错误的问题

c# - 想知道从Web服务返回错误

xamarin.ios - MonoTouch 上的 ServiceStack JIT 错误