这对游戏来说有点晚了,但我遇到了同样的事情,所以开始挖掘源头。实际上有一个简单的解决方法,覆盖HandleResponseException
您正在使用的任何 ServiceClient 中的方法。直接来自评论:
/// <summary>
/// Called by Send method if an exception occurs, for instance a System.Net.WebException because the server
/// returned an HTTP error code. Override if you want to handle specific exceptions or always want to parse the
/// response to a custom ErrorResponse DTO type instead of ServiceStack's ErrorResponse class. In case ex is a
/// <c>System.Net.WebException</c>, do not use
/// <c>createWebRequest</c>/<c>getResponse</c>/<c>HandleResponse<TResponse></c> to parse the response
/// because that will result in the same exception again. Use
/// <c>ThrowWebServiceException<YourErrorResponseType></c> to parse the response and to throw a
/// <c>WebServiceException</c> containing the parsed DTO. Then override Send to handle that exception.
/// </summary>
我个人在覆盖JsonServiceClient
protected override bool HandleResponseException<TResponse>(Exception ex, object request, string requestUri, Func<System.Net.WebRequest> createWebRequest, Func<System.Net.WebRequest, System.Net.WebResponse> getResponse, out TResponse response)
{
Boolean handled;
response = default(TResponse);
try
{
handled = base.HandleResponseException(ex, request, requestUri, createWebRequest, getResponse, out response);
}
catch (WebServiceException webServiceException)
{
if(webServiceException.StatusCode > 0)
throw new HttpException(webServiceException.StatusCode, webServiceException.ErrorMessage);
throw;
}
return handled;
}