Exemplo n.º 1
0
        private void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                OnGetMapServiceDetailsFailed(new ExceptionEventArgs(e.Error, e.UserState));
                return;
            }

            if (string.IsNullOrEmpty(e.Result))
            {
                OnGetMapServiceDetailsFailed(new ExceptionEventArgs(new Exception("Empty response"), e.UserState));
                return;
            }

            MapServiceInfo mapServiceInfo = null;

            try
            {
                string    json = e.Result;
                Exception ex   = createFromJSON(json);
                if (ex != null)
                {
                    OnGetMapServiceDetailsFailed(new ExceptionEventArgs(ex, e.UserState));
                    return;
                }
                byte[] bytes = Encoding.Unicode.GetBytes(json);
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes))
                {
                    DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(MapServiceInfo));
                    mapServiceInfo = dataContractJsonSerializer.ReadObject(memoryStream) as MapServiceInfo;
                    memoryStream.Close();
                }

                if (mapServiceInfo == null)
                {
                    OnGetMapServiceDetailsFailed(new ExceptionEventArgs(new Exception("Unable to deserialize response"), e.UserState));
                    return;
                }
            }
            catch (Exception ex)
            {
                OnGetMapServiceDetailsFailed(new ExceptionEventArgs(ex, e.UserState));
                return;
            }

            // Workaround for the fact that DataContractJsonSerializer doesn't call the default contructor
            // http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx
            if (mapServiceInfo.SpatialReference != null)
            {
                if (mapServiceInfo.SpatialReference.WKID == default(int))
                {
                    mapServiceInfo.SpatialReference.WKID = -1;
                }
            }

            OnGetMapServiceDetailsCompleted(new GetMapServiceDetailsCompletedEventArgs()
            {
                MapServiceInfo = mapServiceInfo, UserState = e.UserState
            });
        }
        private void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                OnGetMapServiceDetailsFailed(new ExceptionEventArgs(e.Error, e.UserState));
                return;
            }

            if (string.IsNullOrEmpty(e.Result))
            {
                OnGetMapServiceDetailsFailed(new ExceptionEventArgs(new Exception("Empty response"), e.UserState));
                return;
            }

            MapServiceInfo mapServiceInfo = null;

            try
            {
                string    json = e.Result;
                Exception ex   = Utils.CheckJsonForException(json);
                if (ex != null)
                {
                    OnGetMapServiceDetailsFailed(new ExceptionEventArgs(ex, e.UserState));
                    return;
                }
                byte[] bytes = Encoding.Unicode.GetBytes(json);
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes))
                {
                    DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(MapServiceInfo));
                    mapServiceInfo = dataContractJsonSerializer.ReadObject(memoryStream) as MapServiceInfo;
                    memoryStream.Close();
                }

                if (mapServiceInfo == null)
                {
                    OnGetMapServiceDetailsFailed(new ExceptionEventArgs(new Exception("Unable to deserialize response"), e.UserState));
                    return;
                }
            }
            catch (Exception ex)
            {
                OnGetMapServiceDetailsFailed(new ExceptionEventArgs(ex, e.UserState));
                return;
            }

            List <Resource> childResources = new List <Resource>();

            if (mapServiceInfo.Layers != null)
            {
                foreach (MapServiceLayerInfo layerInfo in mapServiceInfo.Layers)
                {
                    if (layerInfo.ParentLayerId != -1) // only had layers at the root of the map service node (no parent)
                    {
                        continue;
                    }
                    if (layerInfo.SubLayerIds == null)
                    {
                        childResources.Add(new Resource()
                        {
                            DisplayName  = layerInfo.Name,
                            ResourceType = ResourceType.Layer,
                            Url          = string.Format("{0}/{1}", Uri, layerInfo.ID),
                            Tag          = layerInfo.ID,
                        });
                    }
                    else
                    {
                        childResources.Add(new Resource()
                        {
                            DisplayName  = layerInfo.Name,
                            ResourceType = ResourceType.GroupLayer,
                            Url          = string.Format("{0}/{1}", Uri, layerInfo.ID),
                            Tag          = layerInfo.ID,
                        });
                    }
                }
            }

            // Workaround for the fact that DataContractJsonSerializer doesn't call the default contructor
            // http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx
            if (mapServiceInfo.SpatialReference != null)
            {
                if (mapServiceInfo.SpatialReference.WKID == default(int))
                {
                    mapServiceInfo.SpatialReference.WKID = -1;
                }
            }

            OnGetMapServiceDetailsCompleted(new GetMapServiceDetailsCompletedEventArgs()
            {
                ChildResources = childResources, MapServiceInfo = mapServiceInfo, UserState = e.UserState
            });
        }
        private void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                if (GetMapServiceDetailsFailed != null)
                {
                    GetMapServiceDetailsFailed(this, new ExceptionEventArgs(e.Error, e.UserState));
                }
                return;
            }

            if (string.IsNullOrEmpty(e.Result))
            {
                if (GetMapServiceDetailsFailed != null)
                {
                    GetMapServiceDetailsFailed(this, new ExceptionEventArgs(new Exception("Empty response"), e.UserState));
                }
                return;
            }

            MapServiceInfo mapServiceInfo = null;

            try
            {
                string json  = e.Result;
                byte[] bytes = Encoding.Unicode.GetBytes(json);
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes))
                {
                    DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(MapServiceInfo));
                    mapServiceInfo = dataContractJsonSerializer.ReadObject(memoryStream) as MapServiceInfo;
                    memoryStream.Close();
                }

                if (mapServiceInfo == null)
                {
                    if (GetMapServiceDetailsFailed != null)
                    {
                        GetMapServiceDetailsFailed(this, new ExceptionEventArgs(new Exception("Unable to deserialize response"), e.UserState));
                    }
                    return;
                }
            }
            catch (Exception ex)
            {
                if (GetMapServiceDetailsFailed != null)
                {
                    GetMapServiceDetailsFailed(this, new ExceptionEventArgs(ex, e.UserState));
                }
                return;
            }

            List <Resource> childResources = new List <Resource>();

            if (mapServiceInfo.Layers != null)
            {
                foreach (LayerInfo layerInfo in mapServiceInfo.Layers)
                {
                    if (layerInfo.SubLayerIds == null)
                    {
                        childResources.Add(new Resource()
                        {
                            DisplayName  = layerInfo.Name,
                            ResourceType = ResourceType.Layer,
                            Url          = string.Format("{0}/{1}", Uri, layerInfo.ID),
                        });
                    }
                    else
                    {
                        childResources.Add(new Resource()
                        {
                            DisplayName  = layerInfo.Name,
                            ResourceType = ResourceType.GroupLayer,
                            Url          = string.Format("{0}/{1}", Uri, layerInfo.ID),
                        });
                    }
                }
            }

            if (GetMapServiceDetailsCompleted != null)
            {
                GetMapServiceDetailsCompleted(this, new GetMapServiceDetailsCompletedEventArgs()
                {
                    ChildResources = childResources, MapServiceInfo = mapServiceInfo, UserState = e.UserState
                });
            }
        }
        public void GetBaseMapServiceMetaData(object userToken)
        {
            if (string.IsNullOrEmpty(baseMapServiceUrl) || !Uri.IsWellFormedUriString(baseMapServiceUrl, UriKind.Absolute))
            {
                throw new InvalidOperationException(Resources.Strings.ExceptionMustSpecifyAbsoluteUriForBaseMapServiceUrl);
            }
            MapService mapService = new MapService(baseMapServiceUrl, null)
            {
                ProxyUrl = this.ProxyUrl
            };

            mapService.ServiceDetailsDownloadFailed += (o, e) => {
                if (GetBaseMapServiceMetaDataFailed != null)
                {
                    GetBaseMapServiceMetaDataFailed(this, e);
                }
            };
            mapService.ServiceDetailsDownloadCompleted += (o, e) => {
                MapService service = o as MapService;
                if (service != null)
                {
                    MapServiceInfo mapServiceInfo = service.ServiceInfo;
                    if (mapServiceInfo != null)
                    {
                        // Set spatial reference
                        SpatialReference spatialRef = mapServiceInfo.SpatialReference;
                        MapUnit          mapUnit    = MapUnit.Meters;
                        // Set map unit
                        string mapUnits = mapServiceInfo.Units;
                        if (!string.IsNullOrEmpty(mapUnits))
                        {
                            mapUnits = mapUnits.Replace("esri", string.Empty); // remove esri prefix from map units
                            if (Enum.IsDefined(typeof(MapUnit), mapUnits))
                            {
                                mapUnit = (MapUnit)Enum.Parse(typeof(MapUnit), mapUnits, true);
                            }
                        }

                        if (GetBaseMapServiceMetaDataCompleted != null)
                        {
                            GetBaseMapServiceMetaDataCompleted(this, new GetMapServiceMetaDataCompletedEventArgs()
                            {
                                MapUnit          = mapUnit,
                                SpatialReference = spatialRef,
                                UserState        = e.UserState,
                                FullExtent       = mapServiceInfo.FullExtent,
                                InitialExtent    = mapServiceInfo.InitialExtent,
                                IsCached         = mapServiceInfo.TileInfo != null && mapServiceInfo.SingleFusedMapCache
                            });
                        }
                    }
                    else
                    {
                        if (GetBaseMapServiceMetaDataFailed != null)
                        {
                            GetBaseMapServiceMetaDataFailed(this, new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToRetriveMapServiceInformation), e.UserState));
                        }
                    }
                }
                else
                {
                    if (GetBaseMapServiceMetaDataFailed != null)
                    {
                        GetBaseMapServiceMetaDataFailed(this, new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToRetriveMapServiceInformation), e.UserState));
                    }
                }
            };
            mapService.GetServiceDetails(userToken);
        }