Beispiel #1
0
        protected override void OnProcessMessage(KissTransactionMode mode, MessageQueue queue, Guid conversation)
        {
            string[] extension;
            MessageQueueTransaction msmqTransaction = null;
            var convMessages = LargeMessage.GetMessagesOfConversation(queue, conversation);
            var largeMessage = new LargeMessage(_msmqCapacity, null);
            var dtctoPo      = TimeSpan.FromSeconds(Kiss.GetTimeout(KissTimeout.PO_DTC_STORAGE));
            var sqltoPo      = Kiss.GetTimeout(KissTimeout.PO_SQL_STORAGE);

            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required, dtctoPo))
                    using (var kiss = new SqlConnection(Kiss.GetConnection(_sql)))
                        using (var poStorage =
                                   new SqlCommand(PROCEDURE_PO_STORAGE, kiss)
                        {
                            CommandTimeout = sqltoPo,
                            CommandType = CommandType.StoredProcedure
                        })
                            using (var poRemoval =
                                       new SqlCommand(PROCEDURE_PO_REMOVAL, kiss)
                            {
                                CommandTimeout = sqltoPo,
                                CommandType = CommandType.StoredProcedure
                            })
                            {
                                if (mode == KissTransactionMode.DTC)
                                {
                                    largeMessage.Receive(queue, null, convMessages,
                                                         Kiss.GetTimeout(KissTimeout.PO_RECEIVE_TIMEOUT));
                                }
                                else
                                {
                                    using (var suppress = new TransactionScope(TransactionScopeOption.Suppress))
                                    {
                                        msmqTransaction = new MessageQueueTransaction();
                                        msmqTransaction.Begin();

                                        largeMessage.Receive(queue, msmqTransaction, convMessages,
                                                             Kiss.GetTimeout(KissTimeout.PO_RECEIVE_TIMEOUT));

                                        suppress.Complete();
                                    }
                                }

                                extension = largeMessage.DecodeExtension();
                                kiss.Open();

                                if (bool.Parse(extension[2]))
                                {
                                    poRemoval.Parameters.AddWithValue("@po_id", extension[0]);
                                    poRemoval.ExecuteNonQuery();
                                }
                                else
                                {
                                    poStorage.Parameters.AddWithValue("@po_id", extension[0]);
                                    poStorage.Parameters.AddWithValue("@po_sent", DateTime.FromBinary(long.Parse(extension[1])));
                                    poStorage.Parameters.AddWithValue("@po_received", DateTime.Now);
                                    poStorage.Parameters.AddWithValue("@po", largeMessage.DataBuffer);

                                    poStorage.ExecuteNonQuery();
                                }

                                kiss.Close();
                                scope.Complete();
                            }

                if (mode != KissTransactionMode.DTC)
                {
                    msmqTransaction.Commit();
                }
            }
            catch
            {
                if (msmqTransaction != null && mode == KissTransactionMode.MSMQ)
                {
                    msmqTransaction.Abort();
                }

                throw;
            }
            finally
            {
                if (msmqTransaction != null)
                {
                    msmqTransaction.Dispose();
                }
            }
        }
        protected override void OnProcessMessage(KissTransactionMode mode, MessageQueue queue, Guid conversation)
        {
            string[] extension;
            string   bundleSentChar;
            DateTime bundleSent;
            MessageQueueTransaction msmqTransaction = null;
            var convMessages = LargeMessage.GetMessagesOfConversation(queue, conversation);
            var largeMessage = new LargeMessage(_msmqCapacity, null);
            var dtctoBundle  = TimeSpan.FromSeconds(Kiss.GetTimeout(KissTimeout.BUNDLE_DTC_STORAGE));
            var sqltoBundle  = Kiss.GetTimeout(KissTimeout.BUNDLE_SQL_STORAGE);

            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required, dtctoBundle))
                    using (var kiss = new SqlConnection(Kiss.GetConnection(_sql)))
                        using (var bundleStorage =
                                   new SqlCommand(PROCEDURE_BUNDLE_STORAGE, kiss)
                        {
                            CommandTimeout = sqltoBundle,
                            CommandType = CommandType.StoredProcedure
                        })
                        {
                            if (mode == KissTransactionMode.DTC)
                            {
                                largeMessage.Receive(queue, null, convMessages,
                                                     Kiss.GetTimeout(KissTimeout.BUNDLE_RECEIVE_TIMEOUT));
                            }
                            else
                            {
                                using (var suppress = new TransactionScope(TransactionScopeOption.Suppress))
                                {
                                    msmqTransaction = new MessageQueueTransaction();
                                    msmqTransaction.Begin();

                                    largeMessage.Receive(queue, msmqTransaction, convMessages,
                                                         Kiss.GetTimeout(KissTimeout.BUNDLE_RECEIVE_TIMEOUT));

                                    suppress.Complete();
                                }
                            }

                            extension = largeMessage.DecodeExtension();

                            bundleSent     = DateTime.FromBinary(long.Parse(extension[2]));
                            bundleSentChar = bundleSent.ToString(FORMAT_BUNDLE_SENT, DateTimeFormatInfo.InvariantInfo);

                            bundleStorage.Parameters.AddWithValue("@bundle_id", long.Parse(extension[0]));
                            bundleStorage.Parameters.AddWithValue("@client_id", extension[1]);
                            bundleStorage.Parameters.AddWithValue("@bundle_sent", bundleSent);
                            bundleStorage.Parameters.AddWithValue("@bundle_received", DateTime.Now);
                            bundleStorage.Parameters.AddWithValue("@bundle_sent_char", bundleSentChar);
                            bundleStorage.Parameters.AddWithValue("@bundle", largeMessage.DataBuffer);

                            kiss.Open();
                            bundleStorage.ExecuteNonQuery();
                            kiss.Close();

                            scope.Complete();
                        }

                if (mode != KissTransactionMode.DTC)
                {
                    msmqTransaction.Commit();
                }
            }
            catch
            {
                if (msmqTransaction != null && mode == KissTransactionMode.MSMQ)
                {
                    msmqTransaction.Abort();
                }

                throw;
            }
            finally
            {
                if (msmqTransaction != null)
                {
                    msmqTransaction.Dispose();
                }
            }
        }