public async Task Should_make_the_context_available_to_behaviors()
        {
            await Scenario.Define <Context>()
            .WithEndpoint <Publisher>(b =>
                                      b.When(c => c.Subscriber1Subscribed, session =>
            {
                var options = new PublishOptions();

                options.GetExtensions().Set(new Publisher.PublishExtensionBehavior.Context
                {
                    SomeProperty = "ItWorks"
                });

                return(session.Publish(new MyEvent(), options));
            })
                                      )
            .WithEndpoint <Subscriber1>(b => b.When(async(session, context) =>
            {
                await session.Subscribe <MyEvent>();

                if (context.HasNativePubSubSupport)
                {
                    context.Subscriber1Subscribed = true;
                }
            }))
            .Done(c => c.Subscriber1GotTheEvent)
            .Repeat(r => r.For(Transports.Default))
            .Should(c => Assert.True(c.Subscriber1GotTheEvent))
            .Run();
        }
Esempio n. 2
0
        /// <summary>
        /// Enables the use of custom SqlTransaction instances for publish operations. The same transaction can be used in more than one publish operation.
        /// </summary>
        /// <param name="options">The <see cref="PublishOptions" /> to extend.</param>
        /// <param name="transaction">SqlTransaction instance that will be used by any operations performed by the transport.</param>
        public static void UseCustomSqlTransaction(this PublishOptions options, SqlTransaction transaction)
        {
            // When dispatching, the TransportTransaction is overwritten.
            // The only way for a custom transaction to work is by using immediate dispatch and messages should only appear when the user commits the custom transaction.
            // Which is exactly what will happen after NServiceBus dispatches this message immediately.
            options.RequireImmediateDispatch();

            var transportTransaction = new TransportTransaction();

            transportTransaction.Set(SettingsKeys.IsUserProvidedTransactionKey, true);
            transportTransaction.Set(SettingsKeys.TransportTransactionSqlTransactionKey, transaction);
            options.GetExtensions().Set(transportTransaction);
        }
Esempio n. 3
0
        /// <summary>
        /// Enables the use of custom SqlConnection for publish operations.
        /// </summary>
        /// <param name="options">The <see cref="PublishOptions" /> to extend.</param>
        /// <param name="connection">SqlConnection instance that will be used by any operations performed by the transport.</param>
        public static void UseCustomSqlConnection(this PublishOptions options, SqlConnection connection)
        {
            if (connection == null)
            {
                throw new ArgumentException(nameof(connection));
            }

            options.RequireImmediateDispatch();

            var transportTransaction = new TransportTransaction();

            transportTransaction.Set(SettingsKeys.IsUserProvidedTransactionKey, true);
            transportTransaction.Set(SettingsKeys.TransportTransactionSqlConnectionKey, connection);

            options.GetExtensions().Set(transportTransaction);
        }
    public async Task <string> Post([FromBody] string orderId)
    {
        using (var connection = connectionFactory())
        {
            await connection.OpenAsync().ConfigureAwait(false);

            using (var transaction = connection.BeginTransaction())
            {
                var dataContext = new FrontendDataContext(connection);
                dataContext.Database.UseTransaction(transaction);

                dataContext.Orders.Add(new Order
                {
                    OrderId = orderId
                });

                var message = new OrderAccepted
                {
                    OrderId = orderId
                };
                var options = new PublishOptions();
                var transportTransaction = new TransportTransaction();
                transportTransaction.Set(connection);
                transportTransaction.Set(transaction);
                options.GetExtensions().Set(transportTransaction);
                await messageSession.Publish(message).ConfigureAwait(false);

                await dataContext.SaveChangesAsync().ConfigureAwait(false);

                transaction.Commit();
            }
        }

        log.Info($"Order {orderId} accepted.");
        return($"Order {orderId} accepted.");
    }