/// <summary> /// Opens a readable stream to the specified resource /// </summary> public static Task <ArcGISWebClient.OpenReadCompletedEventArgs> OpenReadTaskAsync( this ArcGISWebClient client, Uri uri, IDictionary <string, string> parameters = null, ArcGISWebClient.HttpMethods httpMethod = ArcGISWebClient.HttpMethods.Auto, object userState = null) { TaskCompletionSource <ArcGISWebClient.OpenReadCompletedEventArgs> tcs = new TaskCompletionSource <ArcGISWebClient.OpenReadCompletedEventArgs>(); EventHandler <ArcGISWebClient.OpenReadCompletedEventArgs> openReadHandler = null; openReadHandler = (o, e) => { client.OpenReadCompleted -= openReadHandler; if (e != null) { tcs.TrySetResult(e); } else { tcs.TrySetException(new Exception(Strings.ExceptionNoResult)); } }; client.OpenReadCompleted += openReadHandler; client.OpenReadAsync(uri, parameters, httpMethod, userState); return(tcs.Task); }
/// <summary> /// Issues a GET request to the specified URI and returns the result /// </summary> public static Task <ArcGISWebClient.DownloadStringCompletedEventArgs> DownloadStringTaskAsync( this ArcGISWebClient client, Uri uri, IDictionary <string, string> parameters, ArcGISWebClient.HttpMethods httpMethod, object userState = null) { TaskCompletionSource <ArcGISWebClient.DownloadStringCompletedEventArgs> tcs = new TaskCompletionSource <ArcGISWebClient.DownloadStringCompletedEventArgs>(); EventHandler <ApplicationUnhandledExceptionEventArgs> unhandledExceptionHandler = null; EventHandler <ArcGISWebClient.DownloadStringCompletedEventArgs> downloadHandler = null; // Handle application unhandled exception due to issue with API where uncatchable deserialization // exception is sometimes thrown for requests that return an error unhandledExceptionHandler = (o, e) => { DownloadStringTaskInProgress = false; Application.Current.UnhandledException -= unhandledExceptionHandler; client.DownloadStringCompleted -= downloadHandler; e.Handled = true; tcs.TrySetException(new Exception(Strings.ExceptionNoResult)); }; downloadHandler = (o, e) => { DownloadStringTaskInProgress = false; if (Application.Current != null) { Application.Current.UnhandledException -= unhandledExceptionHandler; } client.DownloadStringCompleted -= downloadHandler; if (e != null) { tcs.TrySetResult(e); } else { tcs.TrySetException(new Exception(Strings.ExceptionNoResult)); } }; client.DownloadStringCompleted += downloadHandler; if (Application.Current != null) { Application.Current.UnhandledException += unhandledExceptionHandler; } DownloadStringTaskInProgress = true; client.DownloadStringAsync(uri, parameters, httpMethod, userState); return(tcs.Task); }