public async Task Should_use_transaction_in_transport_operations()
        {
            var context = await Scenario.Define <MyContext>()
                          .WithEndpoint <AnEndpoint>(c => c.When(async bus =>
            {
                using (var connection = new SqlConnection(ConnectionString))
                {
                    connection.Open();

                    SendOptions sendOptions;
                    PublishOptions publishOptions;

                    using (var rolledbackTransaction = connection.BeginTransaction())
                    {
                        sendOptions = new SendOptions();
                        sendOptions.UseCustomSqlTransaction(rolledbackTransaction);
                        await bus.Send(new CommandFromRolledbackTransaction(), sendOptions);

                        publishOptions = new PublishOptions();
                        publishOptions.UseCustomSqlTransaction(rolledbackTransaction);
                        await bus.Publish(new EventFromRollbackedTransaction(), publishOptions);

                        rolledbackTransaction.Rollback();
                    }

                    using (var committedTransaction = connection.BeginTransaction())
                    {
                        sendOptions = new SendOptions();
                        sendOptions.UseCustomSqlTransaction(committedTransaction);
                        await bus.Send(new CommandFromCommittedTransaction(), sendOptions);

                        publishOptions = new PublishOptions();
                        publishOptions.UseCustomSqlTransaction(committedTransaction);
                        await bus.Publish(new EventFromCommittedTransaction(), publishOptions);

                        committedTransaction.Commit();
                    }
                }
            }))
                          .Done(c => c.SendFromCommittedTransactionReceived && c.PublishFromCommittedTransactionReceived)
                          .Run(TimeSpan.FromMinutes(1));

            Assert.IsFalse(context.SendFromRolledbackTransactionReceived);
            Assert.IsFalse(context.PublishFromRolledbackTransactionReceived);
        }
                public async Task Handle(InitiatingMessage message, IMessageHandlerContext context)
                {
                    using (var scope = new System.Transactions.TransactionScope(TransactionScopeOption.RequiresNew, TransactionScopeAsyncFlowOption.Enabled))
                    {
                        using (var connection = new SqlConnection(ConnectionString))
                        {
                            connection.Open();

                            using (var rollbackedTransaction = connection.BeginTransaction())
                            {
                                var sendOptions = new SendOptions();
                                sendOptions.UseCustomSqlTransaction(rollbackedTransaction);
                                sendOptions.RouteToThisEndpoint();
                                await context.Send(new FollowUpRolledbackCommand(), sendOptions);

                                var publishOptions = new PublishOptions();
                                publishOptions.UseCustomSqlTransaction(rollbackedTransaction);
                                await context.Publish(new FollowUpRolledbackEvent(), publishOptions);

                                rollbackedTransaction.Rollback();
                            }

                            using (var transaction = connection.BeginTransaction())
                            {
                                var sendOptions = new SendOptions();
                                sendOptions.UseCustomSqlTransaction(transaction);
                                sendOptions.RouteToThisEndpoint();
                                await context.Send(new FollowUpCommittedCommand(), sendOptions);

                                var publishOptions = new PublishOptions();
                                publishOptions.UseCustomSqlTransaction(transaction);
                                await context.Publish(new FollowUpCommittedEvent(), publishOptions);

                                transaction.Commit();
                            }
                        }
                        scope.Complete();
                    }

                    throw new Exception("This should NOT prevent follow-up messages from being sent.");
                }
Esempio n. 3
0
    async Task Usage(IMessageSession session, string connectionString, string commandText)
    {
        #region UseCustomSqlConnectionAndTransaction

        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();

            using (var transaction = connection.BeginTransaction())
            {
                var sqlCommand = new SqlCommand(commandText, connection, transaction);

                //Execute SQL statement
                sqlCommand.ExecuteNonQuery();

                //Send a message
                var sendOptions = new SendOptions();
                sendOptions.UseCustomSqlTransaction(transaction);
                await session.Send(new Message(), sendOptions);

                //Publish a message
                var publishOptions = new PublishOptions();
                publishOptions.UseCustomSqlTransaction(transaction);
                await session.Publish(new Event(), publishOptions);

                transaction.Commit();
            }
        }

        #endregion

        #region UseCustomSqlConnection

        using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew,
                                                TransactionScopeAsyncFlowOption.Enabled))
        {
            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();

                var sqlCommand = new SqlCommand(commandText, connection);

                //Execute SQL statement
                sqlCommand.ExecuteNonQuery();

                //Send a message
                var sendOptions = new SendOptions();
                sendOptions.UseCustomSqlConnection(connection);
                await session.Send(new Message(), sendOptions);

                //Publish a message
                var publishOptions = new PublishOptions();
                publishOptions.UseCustomSqlConnection(connection);
                await session.Publish(new Event(), publishOptions);
            }

            scope.Complete();
        }

        #endregion
    }