private int SendAnnotationsInFolderToDataService(string folderName, string dataServiceUrl)
        {
            if (string.IsNullOrEmpty(folderName) || string.IsNullOrEmpty(dataServiceUrl))
            {
                Platform.Log(LogLevel.Error, "Trying to send queued annotations from an invalid folder ({0}) or to an invalid data service ({1})", folderName, dataServiceUrl);
                return(0);
            }

            string folderPath = System.IO.Path.Combine(AimSettings.Default.AnnotationQueuesFolder, folderName);

            if (!System.IO.Directory.Exists(folderPath))
            {
                return(0);
            }

            int sentCounter = 0;

            try
            {
                string[] annotationFiles = System.IO.Directory.GetFiles(folderPath, "*.xml", System.IO.SearchOption.TopDirectoryOnly);

                if (annotationFiles.Any())
                {
                    IAimDataService aimDataService = new AimDataServiceExtensionPoint().CreateExtensions().OfType <IAimDataService>().FirstOrDefault(dataService => dataService.ServiceUrl == dataServiceUrl);
                    if (aimDataService == null)
                    {
                        Platform.Log(LogLevel.Error, "Trying to send queued annotations to data service at ({0}). There is no data service configured for the URL");
                        return(0);
                    }

                    const int fileCountToSendAtOnce = 10;                     // # of files to read at one time
                    for (int i = 0; i < (annotationFiles.Length + fileCountToSendAtOnce) / fileCountToSendAtOnce; i++)
                    {
                        // Read next batch
                        var annotationPathNames = new List <string>();
                        int nextBatchMaxCnt     = Math.Min((i + 1) * fileCountToSendAtOnce, annotationFiles.Length);
                        for (int j = i * fileCountToSendAtOnce; j < nextBatchMaxCnt; j++)
                        {
                            annotationPathNames.Add(System.IO.Path.Combine(folderPath, annotationFiles[j]));
                        }

                        var xmlAnnotations = AimManager.AimManager.Instance.ReadXmlAnnotationsToMemory(aimDataService.AimVersion, annotationPathNames);

                        // Send read annotations to the Data Service
                        if (xmlAnnotations.Count > 0)
                        {
                            try
                            {
                                aimDataService.SendAnnotations(new List <string>(xmlAnnotations.Values));

                                sentCounter += xmlAnnotations.Count;

                                // Delete sent files
                                Debug.Assert(annotationPathNames.Count > xmlAnnotations.Count, "Was not able to read all annotations");
                                xmlAnnotations.Clear();
                                foreach (var readAnnotationFile in xmlAnnotations.Keys)
                                {
                                    try
                                    {
                                        System.IO.File.Delete(readAnnotationFile);
                                    }
                                    catch (Exception ex)
                                    {
                                        Platform.Log(LogLevel.Error, ex, "Failed to delete sent annotation file ({0}). The file will be send again.", readAnnotationFile);
                                    }
                                }
                                xmlAnnotations.Clear();
                            }
                            catch (Exception ex)
                            {
                                Platform.Log(LogLevel.Debug, ex, "Failed to send annotations to the AIM Data Service ({0}).", dataServiceUrl);

                                break;                                 // assume that data service is unavailable
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Platform.Log(LogLevel.Error, ex, "Failed to send files from {0} to server {1}", folderPath, dataServiceUrl);
            }

            return(sentCounter);
        }
        private int SendAnnotationsInFolderToDataService(string folderName, string dataServiceUrl)
        {
            if (string.IsNullOrEmpty(folderName) || string.IsNullOrEmpty(dataServiceUrl))
            {
                Platform.Log(LogLevel.Error, "Trying to send queued annotations from an invalid folder ({0}) or to an invalid data service ({1})", folderName, dataServiceUrl);
                return 0;
            }

            string folderPath = System.IO.Path.Combine(AimSettings.Default.AnnotationQueuesFolder, folderName);

            if (!System.IO.Directory.Exists(folderPath))
                return 0;

            int sentCounter = 0;
            try
            {
                string[] annotationFiles = System.IO.Directory.GetFiles(folderPath, "*.xml", System.IO.SearchOption.TopDirectoryOnly);

                if (annotationFiles.Any())
                {
                    IAimDataService aimDataService = new AimDataServiceExtensionPoint().CreateExtensions().OfType<IAimDataService>().FirstOrDefault(dataService => dataService.ServiceUrl == dataServiceUrl);
                    if (aimDataService == null)
                    {
                        Platform.Log(LogLevel.Error, "Trying to send queued annotations to data service at ({0}). There is no data service configured for the URL");
                        return 0;
                    }

                    const int fileCountToSendAtOnce = 10; // # of files to read at one time
                    for (int i = 0; i < (annotationFiles.Length + fileCountToSendAtOnce) / fileCountToSendAtOnce; i++)
                    {

                        // Read next batch
                        var annotationPathNames = new List<string>();
                        int nextBatchMaxCnt = Math.Min((i + 1) * fileCountToSendAtOnce, annotationFiles.Length);
                        for (int j = i * fileCountToSendAtOnce; j < nextBatchMaxCnt; j++)
                            annotationPathNames.Add(System.IO.Path.Combine(folderPath, annotationFiles[j]));

                        var xmlAnnotations = AimManager.AimManager.Instance.ReadXmlAnnotationsToMemory(aimDataService.AimVersion, annotationPathNames);

                        // Send read annotations to the Data Service
                        if (xmlAnnotations.Count > 0)
                        {
                            try
                            {
                                aimDataService.SendAnnotations(new List<string>(xmlAnnotations.Values));

                                sentCounter += xmlAnnotations.Count;

                                // Delete sent files
                                Debug.Assert(annotationPathNames.Count > xmlAnnotations.Count, "Was not able to read all annotations");
                                xmlAnnotations.Clear();
                                foreach (var readAnnotationFile in xmlAnnotations.Keys)
                                {
                                    try
                                    {
                                        System.IO.File.Delete(readAnnotationFile);
                                    }
                                    catch (Exception ex)
                                    {
                                        Platform.Log(LogLevel.Error, ex, "Failed to delete sent annotation file ({0}). The file will be send again.", readAnnotationFile);
                                    }
                                }
                                xmlAnnotations.Clear();
                            }
                            catch (Exception ex)
                            {
                                Platform.Log(LogLevel.Debug, ex, "Failed to send annotations to the AIM Data Service ({0}).", dataServiceUrl);

                                break; // assume that data service is unavailable
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Platform.Log(LogLevel.Error, ex, "Failed to send files from {0} to server {1}", folderPath, dataServiceUrl);
            }

            return sentCounter;
        }