Beispiel #1
0
        /// <summary>
        /// Raised when the asynchronous call to search the web has completed.
        /// </summary>
        private void WebSearchCompleted(object sender, OpenReadEventArgs e)
        {
            // Received another response, so increment the counter
            _numberWebResults++;

            if (e.Error == null && !_cancelled) // No error, no cancellation - handle result as normal
            {
                GoogleResponse result = WebUtil.ReadObject <GoogleResponse>(e.Result);
                if (result != null && result.ResponseData != null && result.ResponseData.Items.Length > 0)
                {
                    // before services from the web get added we need to check if they are accessible
                    foreach (WebSearchResultItem item in result.ResponseData.Items)
                    {
                        Uri url = new Uri(item.Url);
                        ArcGISService.GetServiceInfoAsync(item.Url, item, CheckServiceAccessCompleted);
                    }
                    return;
                }
            }

            // There was an error, the response was empty, or the operation was cancelled.  Check whether this is the last response.
            if (_numberWebResults == _NUMREQUESTS)
            {
                // All requests have returned.  Go through completeion routine.

                if (!_cancelled)
                {
                    IsSearching = false; // Reset busy state
                    OnSearchCompleted(); // Raise completed event
                }
            }
        }
        /// <summary>
        /// Gets the service information for an ArcGIS Server service.
        /// </summary>
        /// <param name="url">The URL of the service</param>
        /// <param name="userState">Object to be passed to the callback</param>
        /// <param name="callback">The method to invoke when information retrieval has completed</param>
        /// <param name="proxyUrl">The URL of a proxy server to use when making the request</param>
        public static void GetServiceInfoAsync(string url, object userState, EventHandler <ServiceEventArgs> callback, string proxyUrl = null)
        {
            string jsonUrl = url.Contains("?") ? url + "&f=json" : url + "?f=json";

            WebUtil.OpenReadAsync(new Uri(jsonUrl), url, (sender, e) =>
            {
                if (e.Error != null)
                {
                    callback(null, new ServiceEventArgs());
                    return;
                }
                string requestUrl = (string)e.UserState;

                MapService mapService = WebUtil.ReadObject <MapService>(e.Result);
                if (mapService != null && mapService.Name != null && mapService.Units != null)
                {
                    mapService.Url           = requestUrl;
                    mapService.RequiresProxy = e.UsedProxy;
                    mapService.InitTitle();
                    callback(null, new ServiceEventArgs()
                    {
                        Service = mapService, UserState = userState
                    });
                    return;
                }

                FeatureService featureService = WebUtil.ReadObject <FeatureService>(e.Result);
                if (featureService != null && featureService.Layers != null && featureService.Layers.Length > 0)
                {
                    featureService.Url           = requestUrl;
                    featureService.RequiresProxy = e.UsedProxy;
                    featureService.InitTitle();
                    callback(null, new ServiceEventArgs()
                    {
                        Service = featureService, UserState = userState
                    });
                    return;
                }

                ImageService imageService = WebUtil.ReadObject <ImageService>(e.Result);
                if (imageService != null && imageService.PixelType != null)
                {
                    imageService.Url           = requestUrl;
                    imageService.RequiresProxy = e.UsedProxy;
                    imageService.InitTitle();
                    callback(null, new ServiceEventArgs()
                    {
                        Service = imageService, UserState = userState
                    });
                    return;
                }

                FeatureLayerService featureLayerService = WebUtil.ReadObject <FeatureLayerService>(e.Result);
                if (featureLayerService != null && featureLayerService.Type == "Feature Layer")
                {
                    featureLayerService.Url           = requestUrl;
                    featureLayerService.RequiresProxy = e.UsedProxy;
                    featureLayerService.Title         = featureLayerService.Name;
                    callback(null, new ServiceEventArgs()
                    {
                        Service = featureLayerService, UserState = userState
                    });
                    return;
                }

                LocatorService locatorService = WebUtil.ReadObject <LocatorService>(e.Result);
                if (locatorService != null && locatorService.CandidateFields != null &&
                    (locatorService.AddressFields != null || locatorService.SingleLineAddressField != null))
                {
                    locatorService.Url           = requestUrl;
                    locatorService.RequiresProxy = e.UsedProxy;
                    locatorService.InitTitle();
                    callback(null, new ServiceEventArgs()
                    {
                        Service = locatorService, UserState = userState
                    });
                    return;
                }

                callback(null, new ServiceEventArgs());
            }, proxyUrl);
        }