/// <summary>
        /// Send the executed transaction data to head office, if failed, save the data to file and re-send it later.
        /// </summary>
        public void AddFilialeTransaction(FilialeTransaction filialeTransaction)
        {
            lock (_Lock)
            {
                if (this._FailedQueue.Count > 0)
                {
                    this.AppendFailedTransactionToFile(filialeTransaction);
                    this._FailedQueue.Enqueue(filialeTransaction);
                    this._FailedQueueAddedEvent.Set();
                }
                else
                {
                    try
                    {
                        this.SendFilialeTransactionToExchangeSwitch(filialeTransaction);
                    }
                    catch (Exception exception)
                    {
                        AppDebug.LogEvent("StateServer", String.Format("{0}\r\n{1}", exception, filialeTransaction), EventLogEntryType.Error);

                        this.AppendFailedTransactionToFile(filialeTransaction);
                        this._FailedQueue.Enqueue(filialeTransaction);
                        this._FailedQueueAddedEvent.Set();
                    }
                }
            }
        }
        private void SendFailedTransactions(object state)
        {
            while (true)
            {
                while (this._FailedQueue.Count > 0)
                {
                    FilialeTransaction filialeTransaction = this._FailedQueue.Peek();
                    try
                    {
                        this.SendFilialeTransactionToExchangeSwitch(filialeTransaction);

                        this._FailedQueue.Dequeue();
                        if (this._FailedQueue.Count == 0 && File.Exists(PacketFileFullName))
                        {
                            File.Delete(PacketFileFullName);
                        }
                    }
                    catch (Exception exception)
                    {
                        AppDebug.LogEvent("StateServer", String.Format("{0}\r\n{1}", exception, filialeTransaction), EventLogEntryType.Error);
                        Thread.Sleep(TimeSpan.FromSeconds(SleepSecondsWhileSendFailed));
                    }
                }

                this._FailedQueueAddedEvent.WaitOne();
            }
        }
 private void AppendFailedTransactionToFile(FilialeTransaction filialeTransaction)
 {
     using (FileStream stream = File.Open(PacketFileFullName, FileMode.OpenOrCreate))
     {
         stream.Position = stream.Length;
         this._Formatter.Serialize(stream, filialeTransaction);
     }
 }
        private void SendFilialeTransactionToExchangeSwitch(FilialeTransaction filialeTransaction)
        {
            string filialeCode = ConfigurationManager.AppSettings["FilialeCode"];

            if (String.IsNullOrEmpty(filialeCode))
            {
                throw new Exception("FilialeCode not found in appSettings.");
            }

            this._iExchangeMappingService.FilialeAddTransaction(filialeCode, filialeTransaction.Id, filialeTransaction.XmlTran);
        }
 private void LoadFailedTransactionsFromFile()
 {
     if (File.Exists(PacketFileFullName))
     {
         using (FileStream stream = File.Open(PacketFileFullName, FileMode.Open))
         {
             while (stream.Position < stream.Length)
             {
                 FilialeTransaction item = (FilialeTransaction)this._Formatter.Deserialize(stream);
                 this._FailedQueue.Enqueue(item);
             }
         }
     }
 }