public async Task Should_use_connection_for_all_transport_operations()
        {
            var context = await Scenario.Define <MyContext>()
                          .WithEndpoint <AnEndpoint>(c => c.When(async bus =>
            {
                //HINT: this scope is never committed
                using (new System.Transactions.TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    using (var connection = new SqlConnection(ConnectionString))
                    {
                        await connection.OpenAsync().ConfigureAwait(false);

                        var sendOptions = new SendOptions();
                        sendOptions.UseCustomSqlConnection(connection);
                        await bus.Send(new CommandFromRollbackedScope(), sendOptions);

                        var publishOptions = new PublishOptions();
                        publishOptions.UseCustomSqlConnection(connection);
                        await bus.Publish(new EventFromRollbackedScope(), publishOptions);
                    }
                }

                await bus.SendLocal(new MarkerMessage());
            }))
                          .Done(c => c.MarkerMessageReceived)
                          .Run(TimeSpan.FromMinutes(1));

            Assert.IsFalse(context.SendFromRollbackedScopeReceived);
            Assert.IsFalse(context.PublishFromRollbackedScopeReceived);
        }
        public async Task Should_use_connection_and_not_escalate_to_DTC()
        {
            Guid?transactionId = null;

            await Scenario.Define <MyContext>()
            .WithEndpoint <AnEndpoint>(c => c.When(async bus =>
            {
                using (var completedScope = new System.Transactions.TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    using (var connection = new SqlConnection(ConnectionString))
                    {
                        await connection.OpenAsync().ConfigureAwait(false);

                        var sendOptions = new SendOptions();
                        sendOptions.UseCustomSqlConnection(connection);
                        await bus.Send(new CommandFromCompletedScope(), sendOptions);

                        var publishOptions = new PublishOptions();
                        publishOptions.UseCustomSqlConnection(connection);
                        await bus.Publish(new EventFromCompletedScope(), publishOptions);
                    }

                    transactionId = Transaction.Current.TransactionInformation.DistributedIdentifier;

                    completedScope.Complete();
                }
            }))
            .Done(c => c.SendFromCompletedScopeReceived && c.PublishFromCompletedScopeReceived)
            .Run(TimeSpan.FromMinutes(1));

            Assert.AreEqual(Guid.Empty, transactionId);
        }
                public async Task Handle(InitiatingMessage message, IMessageHandlerContext context)
                {
                    //HINT: this scope is never completed
                    using (new System.Transactions.TransactionScope(TransactionScopeOption.RequiresNew, TransactionScopeAsyncFlowOption.Enabled))
                    {
                        using (var connection = new SqlConnection(ConnectionString))
                        {
                            connection.Open();

                            var sendOptions = new SendOptions();
                            sendOptions.UseCustomSqlConnection(connection);
                            sendOptions.RouteToThisEndpoint();
                            await context.Send(new FollowUpRolledbackCommand(), sendOptions);

                            var publishOptions = new PublishOptions();
                            publishOptions.UseCustomSqlConnection(connection);
                            await context.Publish(new FollowUpRolledbackEvent(), publishOptions);
                        }
                    }

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

                            var sendOptions = new SendOptions();
                            sendOptions.UseCustomSqlConnection(connection);
                            sendOptions.RouteToThisEndpoint();
                            await context.Send(new FollowUpCompletedCommand(), sendOptions);

                            var publishOptions = new PublishOptions();
                            publishOptions.UseCustomSqlConnection(connection);
                            await context.Publish(new FollowUpCompletedEvent(), publishOptions);

                            scenarioContext.InHandlerTransactionEscalatedToDTC = Transaction.Current.TransactionInformation.DistributedIdentifier != Guid.Empty;
                        }

                        scope.Complete();
                    }

                    throw new Exception("This should NOT prevent the InitiatingMessage from failing.");
                }
Esempio n. 4
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
    }