public async void GetLayerDetails(object userState) { if (string.IsNullOrEmpty(Uri)) { throw new InvalidOperationException(Resources.Strings.ExceptionUriMustNotBeNull); } UriBuilder builder = new UriBuilder(Uri); builder.Query = Utils.GetQueryParameters(Uri); finalUrl = builder.Uri; if (webClient == null) { webClient = new ArcGISWebClient(); } webClient.ProxyUrl = ProxyUrl; try { ArcGISWebClient.OpenReadCompletedEventArgs result = await webClient.OpenReadTaskAsync(finalUrl, userState); processResult(result); } catch (Exception ex) { OnGetLayerDetailsFailed(new ExceptionEventArgs(ex, userState)); } }
public OpenReadEventArgs(ArcGISWebClient.OpenReadCompletedEventArgs e) { if (e != null) { Cancelled = e.Cancelled; Error = e.Error; UserState = e.UserState; if (e.Error == null) { Result = e.Result; } } }
// Show a list of map services in the Listbox void webclient_OpenReadCompleted(object sender, ArcGISWebClient.OpenReadCompletedEventArgs e) { try { if (e.Error != null) { throw new Exception(e.Error.Message); } // Deserialize response using classes defined by a data contract, included in this class definition below DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MySvcs)); MySvcs mysvcs = serializer.ReadObject(e.Result) as MySvcs; if (mysvcs.Services.Count == 0) { throw new Exception("No services returned"); } // Use LINQ to return all map services var mapSvcs = from s in mysvcs.Services where s.Type == "MapServer" select s; // If map services are returned, show the Listbox with items as map services if (mapSvcs.Count() > 0) { MySvcTreeView.ItemsSource = mapSvcs; MySvcTreeView.Visibility = System.Windows.Visibility.Visible; NoMapServicesTextBlock.Visibility = System.Windows.Visibility.Collapsed; } else { MySvcTreeView.Visibility = System.Windows.Visibility.Collapsed; NoMapServicesTextBlock.Visibility = System.Windows.Visibility.Visible; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (e.Result != null) { e.Result.Close(); } } }
private void processResult(ArcGISWebClient.OpenReadCompletedEventArgs e) { if (e == null || e.Cancelled) { return; } if (e.Error != null) { OnGetLayerDetailsFailed(new ExceptionEventArgs(e.Error, e.UserState)); return; } if (e.Result == null) { OnGetLayerDetailsFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionEmptyResponse), e.UserState)); return; } string json = (new StreamReader(e.Result)).ReadToEnd(); if (string.IsNullOrEmpty(json)) { OnGetLayerDetailsFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionEmptyResponse), e.UserState)); return; } LayerDetails layerDetails; try { Exception exception = Utils.CheckJsonForException(json); if (exception != null) { OnGetLayerDetailsFailed(new ExceptionEventArgs(exception, e.UserState)); return; } byte[] bytes = Encoding.Unicode.GetBytes(json); using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes)) { DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(LayerDetails)); layerDetails = dataContractJsonSerializer.ReadObject(memoryStream) as LayerDetails; memoryStream.Close(); } if (layerDetails == null) { OnGetLayerDetailsFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToDeserializeResponse), e.UserState)); return; } } catch (Exception ex) { OnGetLayerDetailsFailed(new ExceptionEventArgs(ex, e.UserState)); return; } List <Resource> childResources = new List <Resource>(); if (layerDetails.Fields != null) { foreach (ESRI.ArcGIS.Mapping.Core.Field field in layerDetails.Fields) { childResources.Add(new Resource() { ResourceType = ResourceType.Field, DisplayName = field.Alias, ProxyUrl = ProxyUrl, Url = field.Type, }); } } OnGetLayerDetailsCompleted(new GetLayerDetailsCompletedEventArgs() { ChildResources = childResources, LayerDetails = layerDetails, UserState = e.UserState }); }