private void FetchAndStoreContents(ChorusAccount chorusAccount, Uri requestUri) { ChorusContents contents = new ChorusContents(); var key = new RequestKey(chorusAccount, requestUri); try { StoreContentsResponse(key, new ChorusContentsResponse(FetchContents(chorusAccount, requestUri), true)); } catch (Exception exception) { ChorusServerException chorusException = exception as ChorusServerException; // ReSharper disable once ConvertIfStatementToNullCoalescingExpression if (null == chorusException) { chorusException = new ChorusServerException( Resources.ChorusSession_FetchContents_There_was_an_error_communicating_with_the_server__ + exception.Message, exception); } StoreContentsResponse(key, new ChorusContentsResponse(contents, true) { ChorusException = chorusException, }); } finally { lock (_lock) { _fetchRequests.Remove(key); } } }
public static ChorusServerException WrapWebException(WebException webException) { string httpErrorMessage = Resources.ChorusSession_WrapWebException_An_error_occurred_communicating_with_the_server___The_server_return_HTTP_error_response_code__0__; try { if (null == webException.Response) { return(new ChorusServerException(string.Format(httpErrorMessage, webException.Status), webException)); // Not L10N } using (var responseStream = webException.Response.GetResponseStream()) { if (null == responseStream) { return (new ChorusServerException( string.Format(httpErrorMessage, webException.Status), webException)); } MemoryStream memoryStream = new MemoryStream(); int count; byte[] buffer = new byte[65536]; while ((count = responseStream.Read(buffer, 0, buffer.Length)) != 0) { memoryStream.Write(buffer, 0, count); } String fullMessage = Encoding.UTF8.GetString(memoryStream.ToArray()); var xmlSerializer = new XmlSerializer(typeof(ChorusErrorResponse)); ChorusErrorResponse chorusErrorResponse; try { chorusErrorResponse = (ChorusErrorResponse)xmlSerializer.Deserialize(new StringReader(fullMessage)); } catch (Exception) { // If there is an error in the XML of the response, then put the full text of the response in an inner exception, // and return them an error. var innerException = new ChorusServerException(fullMessage, webException); return(new ChorusServerException(string.Format(Resources.ChorusSession_WrapWebException_An_error_occurred_communicating_with_the_server___The_server_returned_the_HTTP_error_response_code__0__but_the_error_message_could_not_be_parsed_, webException.Status), innerException)); } if (!string.IsNullOrEmpty(chorusErrorResponse.StackTrace)) { Trace.TraceWarning(chorusErrorResponse.StackTrace); } return(new ChorusServerException(chorusErrorResponse.Message, webException)); } } catch (Exception exception) { return(new ChorusServerException(exception.Message, new AggregateException(webException, exception))); } }
public ChorusContentsResponse(ChorusServerException exception) { ChorusException = exception; }
public bool AsyncFetchContents(ChorusAccount chorusAccount, ChorusUrl chorusUrl, out ChorusServerException chorusException) { Uri requestUri = GetContentsUri(chorusAccount, chorusUrl); if (null == requestUri) { chorusException = null; return(true); } RequestKey requestKey = new RequestKey(chorusAccount, requestUri); lock (_lock) { ChorusContentsResponse chorusContentsResponse; if (_chorusContentsByServerUrl.TryGetValue(requestKey, out chorusContentsResponse)) { chorusException = chorusContentsResponse.ChorusException; return(chorusContentsResponse.IsComplete); } chorusException = null; if (!_fetchRequests.Add(requestKey)) { return(false); } } ActionUtil.RunAsync(() => FetchAndStoreContents(chorusAccount, requestUri), "Fetch from Chorus"); // Not L10N return(false); }