示例#1
0
        async Task <StartupCheckResult> CheckForAmbientTransactionEnlistmentSupport(SqlConnectionFactory connectionFactory, TransactionOptions transactionOptions)
        {
            if (!settings.TryGet(out TransportTransactionMode requestedTransportTransactionMode))
            {
                requestedTransportTransactionMode = TransactionMode;
            }

            if (requestedTransportTransactionMode == TransportTransactionMode.TransactionScope)
            {
                try
                {
                    using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOptions, TransactionScopeAsyncFlowOption.Enabled))
                        using (await connectionFactory.OpenNewConnection().ConfigureAwait(false))
                        {
                            scope.Complete();
                        }
                }
                catch (NotSupportedException)
                {
                    var message = "The version of System.Data.SqlClient in use does not support enlisting SQL connections in ambient transactions, so the TransactionScope transport transaction mode cannot be used. Use `EndpointConfiguration.UseTransport<SqlServerTransport>().Transactions` to select a different transport transaction mode.";
                    return(StartupCheckResult.Failed(message));
                }
            }

            return(StartupCheckResult.Success);
        }
        ExpiredMessagesPurger CreateExpiredMessagesPurger(SqlConnectionFactory connectionFactory)
        {
            var purgeTaskDelay = settings.HasSetting(SettingsKeys.PurgeTaskDelayTimeSpanKey) ? settings.Get <TimeSpan?>(SettingsKeys.PurgeTaskDelayTimeSpanKey) : null;
            var purgeBatchSize = settings.HasSetting(SettingsKeys.PurgeBatchSizeKey) ? settings.Get <int?>(SettingsKeys.PurgeBatchSizeKey) : null;

            return(new ExpiredMessagesPurger(_ => connectionFactory.OpenNewConnection(), purgeTaskDelay, purgeBatchSize));
        }
示例#3
0
        public async Task Subscribe(string endpointName, string queueAddress, string topic)
        {
            using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
            {
                using (var connection = await connectionFactory.OpenNewConnection().ConfigureAwait(false))
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = subscribeCommand;
                        command.Parameters.Add("Endpoint", SqlDbType.VarChar).Value     = endpointName;
                        command.Parameters.Add("QueueAddress", SqlDbType.VarChar).Value = queueAddress;
                        command.Parameters.Add("Topic", SqlDbType.VarChar).Value        = topic;

                        await command.ExecuteNonQueryAsync().ConfigureAwait(false);
                    }
            }
        }
示例#4
0
        ExpiredMessagesPurger CreateExpiredMessagesPurger(SqlConnectionFactory connectionFactory)
        {
            var purgeBatchSize = settings.HasSetting(SettingsKeys.PurgeBatchSizeKey) ? settings.Get <int?>(SettingsKeys.PurgeBatchSizeKey) : null;
            var enable         = settings.GetOrDefault <bool>(SettingsKeys.PurgeEnableKey);

            diagnostics.Add("NServiceBus.Transport.SqlServer.ExpiredMessagesPurger", new
            {
                FeatureEnabled = enable,
                BatchSize      = purgeBatchSize
            });

            return(new ExpiredMessagesPurger(_ => connectionFactory.OpenNewConnection(), purgeBatchSize, enable));
        }
示例#5
0
        public async Task CreateIfNecessary()
        {
            using (var connection = await connectionFactory.OpenNewConnection().ConfigureAwait(false))
                using (var transaction = connection.BeginTransaction())
                {
#pragma warning disable 618
                    var sql = string.Format(SqlConstants.CreateSubscriptionTableText, tableName.QuotedQualifiedName, tableName.QuotedCatalog);
#pragma warning restore 618
                    using (var command = new SqlCommand(sql, connection, transaction)
                    {
                        CommandType = CommandType.Text
                    })
                    {
                        await command.ExecuteNonQueryAsync().ConfigureAwait(false);
                    }
                    transaction.Commit();
                }
        }