public void Send(TransportMessage message, SendOptions sendOptions) { Address destination = null; try { destination = DetermineDestination(sendOptions); var connectionInfo = connectionStringProvider.GetForDestination(sendOptions.Destination); var queue = new TableBasedQueue(destination, connectionInfo.Schema); if (sendOptions.EnlistInReceiveTransaction) { SqlTransaction currentTransaction; if (connectionStore.TryGetTransaction(connectionInfo.ConnectionString, out currentTransaction)) { queue.Send(message, sendOptions, currentTransaction.Connection, currentTransaction); } else { SqlConnection currentConnection; if (connectionStore.TryGetConnection(connectionInfo.ConnectionString, out currentConnection)) { queue.Send(message, sendOptions, currentConnection); } else { using (var connection = sqlConnectionFactory.OpenNewConnection(connectionInfo.ConnectionString)) { queue.Send(message, sendOptions, connection); } } } } else { // Suppress so that even if DTC is on, we won't escalate using (var tx = new TransactionScope(TransactionScopeOption.Suppress)) { using (var connection = sqlConnectionFactory.OpenNewConnection(connectionInfo.ConnectionString)) { queue.Send(message, sendOptions, connection); } tx.Complete(); } } } catch (SqlException ex) { if (ex.Number == 208) { ThrowQueueNotFoundException(destination, ex); } ThrowFailedToSendException(destination, ex); } catch (Exception ex) { ThrowFailedToSendException(destination, ex); } }
static async Task SendMessage(TableBasedQueue tableBasedQueue) { await ExecuteInTransactionScope(async c => { var message = new OutgoingMessage(Guid.NewGuid().ToString(), new Dictionary <string, string>(), new byte[0]); await tableBasedQueue.Send(message, TimeSpan.MaxValue, c, null); }); }
static async Task SendMessage(TableBasedQueue tableBasedQueue, SqlConnectionFactory sqlConnectionFactory) { using (var scope = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled)) { using (var connection = await sqlConnectionFactory.OpenNewConnection()) using (var tx = connection.BeginTransaction()) { var message = new OutgoingMessage(Guid.NewGuid().ToString(), new Dictionary <string, string>(), new byte[0]); await tableBasedQueue.Send(message, TimeSpan.MaxValue, connection, tx); tx.Commit(); scope.Complete(); } } }