示例#1
0
        private void processResult(ArcGISWebClient.DownloadStringCompletedEventArgs e) 
        {
            if (e.Cancelled)
                return;

            if (e.Error != null)
            {
                OnGetCatalogFailed(new ExceptionEventArgs(e.Error, e.UserState));
                return;
            }
            if (string.IsNullOrEmpty(e.Result))
            {
                OnGetCatalogFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionEmptyResponse), e.UserState));                                        
                return;
            }

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

            if (catalog == null) 
            { 
                OnGetCatalogFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToDeserializeCatalog), e.UserState));   
                return; 
            }

            if (catalog.Folders == null && catalog.Services == null)
            {
                OnGetCatalogFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionNoServicesFoundOnServer), e.UserState));
                return; 
            }

            List<Resource> childResources = new List<Resource>();
            List<Resource> resources = Utility.GetResources(catalog, Filter, Uri, ProxyUrl, lockObj);
            int childCount = 0;
            int totalChildCount = resources != null ? resources.Count : 0;

            // If the catalog has any folders, add them to the returned list first
            if (catalog.Folders != null)
            {
                totalChildCount += catalog.Folders.Count;
                foreach (string folderName in catalog.Folders)
                {
                    Folder folder = new Folder(string.Format("{0}/{1}", Uri, folderName), ProxyUrl)
                    {
                        Filter = Filter,
                    };
                    folder.GetServicesInFolderFailed += (o, args) =>
                    {
                        // Remove the folder
                        lock (lockObj)
                        {
                            childResources.Remove(args.UserState as Resource);
                        }

                        childCount++;
                        if (childCount >= totalChildCount)
                            OnGetCatalogCompleted(new GetCatalogCompletedEventArgs() { ChildResources = childResources, UserState = e.UserState });
                    };
                    folder.GetServicesInFolderCompleted += (o, args) =>
                    {
                        int nestedChildTotalCount = args.ChildResources.Count();
                        if (nestedChildTotalCount == 0)
                        {
                            // Remove the folder
                            lock (lockObj)
                            {
                                childResources.Remove(args.UserState as Resource);
                            }
                        }

                        childCount++;
                        if (childCount >= totalChildCount)
                            OnGetCatalogCompleted(new GetCatalogCompletedEventArgs() { ChildResources = childResources, UserState = e.UserState });
                    };

                    // Add the folder before validation so that the catalog order is preserved.  Folder will be
                    // removed if found to be invalid.
                    Resource folderResource = new Resource()
                    {
                        DisplayName = folderName,
                        Url = string.Format("{0}/{1}", Uri, folderName),
                        ProxyUrl = ProxyUrl,
                        ResourceType = ResourceType.Folder,
                    };
                    lock (lockObj)
                    {
                        childResources.Add(folderResource);
                    }
                    folder.GetServices(folderResource);
                }
            }

            // Remove any undesired services due to filtering
            foreach (Resource childRes in resources)
            {
                IService childService = ServiceFactory.CreateService(childRes.ResourceType, childRes.Url, childRes.ProxyUrl);
                if (childService != null)
                {
                    // Determine if filtering requires detailed information about the server and only make async call to
                    // obtain detailed info if true.
                    if (childService.IsServiceInfoNeededToApplyThisFilter(Filter))
                    {
                        childService.ServiceDetailsDownloadFailed += (o, args) =>
                        {
                            // Remove resource
                            lock (lockObj)
                            {
                                childResources.Remove(args.UserState as Resource);
                            }

                            childCount++;
                            if (childCount >= totalChildCount)
                                OnGetCatalogCompleted(new GetCatalogCompletedEventArgs() { ChildResources = childResources, UserState = e.UserState });
                        };
                        childService.ServiceDetailsDownloadCompleted += (o, args) =>
                        {
                            IService service = o as IService;
                            if (service == null || !service.IsFilteredIn(Filter)) // check if service is filtered
                            {
                                // Remove resource
                                lock (lockObj)
                                {
                                    childResources.Remove(args.UserState as Resource);
                                }
                            }
                            childCount++;
                            if (childCount >= totalChildCount)
                                OnGetCatalogCompleted(new GetCatalogCompletedEventArgs() { ChildResources = childResources, UserState = e.UserState });
                        };

                        // Add the service before validation so that the catalog order is preserved.  Service will be
                        // removed if found to be invalid.
                        Resource childResource = new Resource()
                        {
                            DisplayName = childRes.DisplayName,
                            Url = childService.Uri,
                            ProxyUrl = ProxyUrl,
                            ResourceType = childService.Type
                        };
                        lock (lockObj)
                        {
                            childResources.Add(childResource);
                        }
                        childService.GetServiceDetails(childResource);
                    }
                    else
                    {
                        // Apply filtering using basic information, not detailed information
                        if (childService != null && childService.IsFilteredIn(Filter))
                        {
                            lock (lockObj)
                            {
                                childResources.Add(new Resource()
                                {
                                    DisplayName = childRes.DisplayName,
                                    Url = childService.Uri,
                                    ProxyUrl = ProxyUrl,
                                    ResourceType = childService.Type
                                });
                            }
                        }
                        ++childCount;
                    }
                }
            }

            if (childCount >= totalChildCount)
                OnGetCatalogCompleted(new GetCatalogCompletedEventArgs() { ChildResources = childResources, UserState = e.UserState });
        }
 private void getServicesInFolder(Resource parentResource, Filter filter, object userState)
 {
     folder = new Folder(parentResource.Url, parentResource.ProxyUrl) { Filter = filter };
     folder.GetServicesInFolderCompleted += (o, e) =>
     {
         OnGetChildResourcesCompleted(new GetChildResourcesCompletedEventArgs() { ChildResources = e.ChildResources, UserState = e.UserState });
     };
     folder.GetServicesInFolderFailed += (o, e) =>
     {
         OnGetChildResourcesFailed(e);
     };
     folder.GetServices(userState);
 }