コード例 #1
0
        }     // END METHOD

        /// <summary>
        /// ECGridOSDownload - Downloads Files using ECGridOS ParcelInBox() or ParcelInBoxEx()
        /// </summary>
        /// <param name="APIKey">string GUID</param>
        /// <param name="FileDir">string</param>
        /// <param name="ECGridIDFrom">integer</param>
        /// <param name="ECGridIDTo">integer</param>
        private static void ECGridOSDownload(string APIKey, string FileDir, int ECGridIDFrom, int ECGridIDTo)
        {
            // ECGRIDOS: Set up an reference for the Web Service this requires a Web Reference to https://ecgridos.net/v3.2/prod/ecgridos.asmx
            net.ecgridos.ECGridOSAPIv3 ecgridos = new net.ecgridos.ECGridOSAPIv3();
            ecgridos.EnableDecompression = true;
            long ParcelID;

            // The FileInfo object holds all the info and payload of the downloaded file
            ECGridOS_API.FileInfo FileInfo = new ECGridOS_API.FileInfo();
            ECGridOS_API.ParcelIDInfoCollection Parcels = new ECGridOS_API.ParcelIDInfoCollection();

            // Add trailing slash onto directory
            if (!FileDir.Right(1).Equals(@"\"))
            {
                FileDir += "\\";
            }

            ECGridOSConsole("WhoAmI(\"{0}\")", APIKey);
            ECGridOS_API.SessionInfo whoami = ecgridos.WhoAmI(APIKey);

            if (ECGridIDFrom > 0)
            {
                ECGridOSConsole("ParcelInBoxEx(\"{0}\",{1},{2},{3},{4})", APIKey, whoami.NetworkID, whoami.MailboxID, ECGridIDFrom, ECGridIDTo);
                Parcels = ecgridos.ParcelInBoxEx(APIKey, whoami.NetworkID, whoami.MailboxID, ECGridIDFrom, ECGridIDTo);
            }
            else
            {
                ECGridOSConsole("ParcelInBox(\"{0}\")", APIKey);
                Parcels = ecgridos.ParcelInBox(APIKey);
            } // END IF/ELSE

            // Try to download each Parcel from the Inbox
            foreach (ECGridOS_API.ParcelIDInfo Parcel in Parcels.ParcelIDInfoList)
            {
                Console.WriteLine("Downloading: {0}  ParcelID: {1}  Bytes: {2}", Parcel.FileName, Parcel.ParcelID, Parcel.ParcelBytes);
                ParcelID = Parcel.ParcelID;

                // Check the Size of this file and use GZip decompression if it is 100 KB or over
                if (Parcel.ParcelBytes <= 100000)
                {
                    // ECGRIDOS: ParcelDownloadGZip() is used to download the File info & content, it returns the FileInfo object
                    ECGridOSConsole("ParcelDownload(\"{0}\",{1})", APIKey, ParcelID);
                    FileInfo = ecgridos.ParcelDownload(APIKey, ParcelID);
                }
                else
                {
                    // ECGRIDOS: ParcelDownloadGZip() is used to download the File info & content, it returns the FileInfo object
                    ECGridOSConsole("ParcelDownloadGZip(\"{0}\",{1})", APIKey, ParcelID);
                    FileInfo = ecgridos.ParcelDownloadGZip(APIKey, ParcelID);
                }

                string errorMessage = "";
                try
                {
                    if (IsGzipCompressed(FileInfo.Content))
                    {
                        byte[] decompressedContent = GzipDecompress(FileInfo.Content);

                        // Write to File to Drive
                        File.WriteAllBytes(Path.Combine(FileDir, FileInfo.FileName), decompressedContent);
                    }
                    else
                    {
                        // Write to File to Drive
                        File.WriteAllBytes(Path.Combine(FileDir, FileInfo.FileName), FileInfo.Content);
                    }

                    ECGridOSConsole("ParcelDownloadConfirm(\"{0}\",{1})", APIKey, ParcelID);
                    ecgridos.ParcelDownloadConfirm(APIKey, ParcelID);

                    Console.WriteLine("Complete.");
                    if (targetAnalytics && whoami.NetworkID == 506)
                    {
                        TargetConfirm(APIKey, Parcel);
                    }
                }
                catch (SoapException SoapEx)
                {
                    // There is good data in the InnerXML which can be parsed and used to processes specific exceptions
                    errorMessage = ShowSoapError(SoapEx);
                    returnValue  = 5;
                }
                catch (Exception ex)
                {
                    errorMessage = "ERROR: " + ex.ToString();
                    Console.WriteLine(errorMessage);
                    returnValue = 2;
                }

                // Chck if there are any error messsages
                if (errorMessage.Length > 0)
                {
                    SendError(ErrorContact, errorMessage);
                    ECGridOSConsole("ParcelDownloadReset(\"{0}\",{1})", APIKey, ParcelID);

                    try
                    {
                        // Reset the Download if error
                        ecgridos.ParcelDownloadReset(APIKey, ParcelID);
                        Console.WriteLine("error: download reset.");
                    }
                    catch (SoapException SoapEx)
                    {
                        errorMessage = ShowSoapError(SoapEx);
                    }
                } // END IF
            }     // END FOREACH
        }         // END METHOD
コード例 #2
0
        /// <summary>
        /// ECGridOSUpload - Uploads a File using ECGridOS ParcelUpload()/ParcelUploadGZip() or ParcelUploadDirected()/ParcelUploadDirectedGZip()
        /// </summary>
        /// <param name="APIKey">string GUID</param>
        /// <param name="FileDir">string</param>
        /// <param name="ECGridIDFrom">integer</param>
        /// <param name="ECGridIDTo">integer</param>
        private static void ECGridOSUpload(string APIKey, string FileDir, int ECGridIDFrom, int ECGridIDTo)
        {
            // Declare variables
            IReadOnlyCollection <string> files;
            int ParcelID;

            // Check if this File or Directory Exists
            if (File.Exists(FileDir) || Directory.Exists(FileDir))
            {
                // Find out if this is a file or directory
                FileAttributes attributes = File.GetAttributes(FileDir);
                if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    // This is a directory
                    files = Directory.GetFiles(FileDir, "*.*", SearchOption.TopDirectoryOnly);
                }
                else
                {
                    // This is a single file
                    files = Directory.GetFiles(Path.GetDirectoryName(FileDir), Path.GetFileName(FileDir), SearchOption.TopDirectoryOnly);
                }
            }
            else
            {
                returnValue = 2;
                throw new IOException("File/Path not found");
            }

            // Check if there are ECGridIDs and Get the SessionInfo if True
            bool isDirected = false;

            ECGridOS_API.SessionInfo whoami = new ECGridOS_API.SessionInfo();
            if (ECGridIDFrom > 0)
            {
                // ECGRIDOS: Set up an reference for the Web Service this requires a Web Reference to https://ecgridos.net/v3.2/prod/ecgridos.asmx
                using (net.ecgridos.ECGridOSAPIv3 ecgridos = new net.ecgridos.ECGridOSAPIv3())
                {
                    ecgridos.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["TimeOut"].ToString());

                    // ECGRIDOS: ParcelUploadDirected() posts a file to ECGrid wrapped with specified From and To IDs.
                    // The ParcelID can be used as a handle later to get more information about the specific files as it transits the system.
                    ECGridOSConsole("WhoAmI(\"{0}\")", APIKey);
                    whoami     = ecgridos.WhoAmI(APIKey);
                    isDirected = true;
                }
            }

            // Upload File(s)
            foreach (string fileName in files)
            {
                //Load the entire file into a string buffer
                byte[] buffer = File.ReadAllBytes(fileName);

                // Check that the file is not empty
                if (buffer.Length <= 0)
                {
                    Console.WriteLine("Deleting Empty File: {0} ....", fileName);
                    File.Delete(fileName);
                    continue;
                }

                Console.WriteLine("Uploading: {0} ....", fileName);

                if (isDirected)
                {
                    // ECGRIDOS: Set up an reference for the Web Service this requires a Web Reference to https://ecgridos.net/v3.2/prod/ecgridos.asmx
                    using (net.ecgridos.ECGridOSAPIv3 ecgridos = new net.ecgridos.ECGridOSAPIv3())
                    {
                        ecgridos.Timeout             = Convert.ToInt32(ConfigurationManager.AppSettings["TimeOut"].ToString());
                        ecgridos.EnableDecompression = true;

                        // Check the Size of this file and use GZip compression if it is 1 MB or over

                        /*
                         * if (buffer.Length >= 100000)
                         * {
                         *  buffer = GzipCompress(buffer);
                         *  Console.WriteLine("GZip Compressing File: {0} ....", fileName);
                         *
                         *  // ECGRIDOS: ParcelUploadDirectedGZip() posts a GZip file to ECGrid
                         *  // The ParcelID can be used as a handle later to get more information about the specific files as it transits the system.
                         *  ECGridOSConsole("ParcelUploadDirectedGZip(\"{0}\",{1},{2},\"{3}\",{4},{5},{6},{7})", APIKey, whoami.NetworkID, whoami.MailboxID, Path.GetFileName(fileName), buffer.Length, "byte[]", ECGridIDFrom, ECGridIDTo);
                         *  ParcelID = ecgridos.ParcelUploadDirectedGZip(APIKey, whoami.NetworkID, whoami.MailboxID, Path.GetFileName(fileName), buffer.Length, buffer, ECGridIDFrom, ECGridIDTo);
                         * }
                         * else
                         * {
                         */
                        // ECGRIDOS: ParcelUploadDirected() posts a GZip file to ECGrid
                        // The ParcelID can be used as a handle later to get more information about the specific files as it transits the system.
                        ECGridOSConsole("ParcelUploadDirected(\"{0}\",{1},{2},\"{3}\",{4},{5},{6},{7})", APIKey, whoami.NetworkID, whoami.MailboxID, Path.GetFileName(fileName), buffer.Length, "byte[]", ECGridIDFrom, ECGridIDTo);
                        ParcelID = ecgridos.ParcelUploadDirected(APIKey, whoami.NetworkID, whoami.MailboxID, Path.GetFileName(fileName), buffer.Length, buffer, ECGridIDFrom, ECGridIDTo);
                        //}
                    }
                }
                else
                {
                    // ECGRIDOS: Set up an reference for the Web Service this requires a Web Reference to https://ecgridos.net/v3.2/prod/ecgridos.asmx
                    using (net.ecgridos.ECGridOSAPIv3 ecgridos = new net.ecgridos.ECGridOSAPIv3())
                    {
                        ecgridos.Timeout             = Convert.ToInt32(ConfigurationManager.AppSettings["TimeOut"].ToString());
                        ecgridos.EnableDecompression = true;

                        // Check the Size of this file and use GZip compression if it is 1 MB or over
                        if (buffer.Length >= 1000000)
                        {
                            buffer = GzipCompress(buffer);
                            Console.WriteLine("GZip Compressing File: {0} ....", fileName);

                            // ECGRIDOS: ParcelUploadGZip() posts a GZip EDI file to ECGrid
                            // The ParcelID can be used as a handle later to get more information about the specific files as it transits the system.
                            ECGridOSConsole("ParcelUploadGZip(\"{0}\",\"{1}\",{2},{3})", APIKey, Path.GetFileName(fileName), buffer.Length, "byte[]");
                            ParcelID = ecgridos.ParcelUploadGZip(APIKey, Path.GetFileName(fileName), buffer.Length, buffer);
                        }
                        else
                        {
                            // ECGRIDOS: ParcelUpload() posts a EDI file to ECGrid
                            // The ParcelID can be used as a handle later to get more information about the specific files as it transits the system.
                            ECGridOSConsole("ParcelUpload(\"{0}\",\"{1}\",{2},{3})", APIKey, Path.GetFileName(fileName), buffer.Length, "byte[]");
                            ParcelID = ecgridos.ParcelUpload(APIKey, Path.GetFileName(fileName), buffer.Length, buffer);
                        }
                    }
                } // END IF/ELSE

                Console.WriteLine("ParcelID = {0}", ParcelID);

                // Delete the file after a successful upload.
                // Note that the Try/Catch from the calling routine will handle any errors and not delete the file if it doesn't upload
                File.Delete(fileName);
            } // END FOREACH(File)
        }     // END METHOD