這是比較晚的遊戲,但我遇到了同樣的事情,所以開始挖掘源代碼。實際上有一個簡單的解決方法,覆蓋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;
}
覆蓋以下