private void Success <TModel>(string response, WrappedAsyncCompletedEventArgs e) { var value = JsonConvert.DeserializeObject <TModel>(response, new StringEnumConverter()); HttpException exception = value != null ? null : new HttpException((int)HttpStatusCode.NoContent, string.Format("An error occurred while deserializing the response: {0}", response)); HydrateCompletedEventArgs args = new HydrateCompletedEventArgs(typeof(TModel), value, exception, false, e.UserState, e.OriginalUrl, response); OnHydrateCompleted <TModel>(args); }
private void ValidateStringResult <TModel>(object sender, WrappedAsyncCompletedEventArgs e, string result) { // check for error if (e.Error != null) { OnHydrateCompleted <TModel>(new HydrateCompletedEventArgs <TModel>(default(TModel), e.Error, e.Cancelled, e.UserState, e.OriginalUrl, result)); } // check response type var headers = ((IWebHelper)sender).ResponseHeaders != null ? ((IWebHelper)sender).ResponseHeaders.GetValues("Content-Type") : null; if (headers != null && headers.Any(x => x.Contains("text/html"))) { OnHydrateCompleted <TModel>(new HydrateCompletedEventArgs <TModel>(default(TModel), new HttpException((int)HttpStatusCode.UnsupportedMediaType, string.Format( "The underlying web service located at {0} returned HTML and only JSON or XML is supported.", e.OriginalUrl)), e.Cancelled, e.UserState, e.OriginalUrl, result)); } // check for empty response string json = result; if (string.IsNullOrEmpty(json)) { OnHydrateCompleted <TModel>(new HydrateCompletedEventArgs <TModel>(default(TModel), new HttpException((int)HttpStatusCode.NoContent, string.Format( "The underlying web service response from {0} was null.", e.OriginalUrl)), e.Cancelled, e.UserState, e.OriginalUrl, result)); } else if (e.OriginalUrl != null && e.OriginalUrl.AbsoluteUri.ToString().ToLower().Contains("arcgis/rest/services")) { ESRIException error = JsonConvert.DeserializeObject <ESRIException>(json); if (error.Error != null) { OnHydrateCompleted <TModel>(new HydrateCompletedEventArgs <TModel>(default(TModel), new HttpException((int)error.Error.Code, string.Format( "The underlying ArcGIS service located at {0} returned the following error: {1}: {2}", e.OriginalUrl, error.Error.Message, string.Join("; ", error.Error.Details))), e.Cancelled, e.UserState, e.OriginalUrl, result)); } else { Success <TModel>(json, e); } } else { Success <TModel>(json, e); } }
private void ValidateByteArrayResult <TModel>(object sender, WrappedAsyncCompletedEventArgs e, byte[] result) { var strResult = result == null ? string.Empty : ((IWebHelper)sender).Encoding.GetString(result); ValidateStringResult <TModel>(sender, e, strResult); }