private void ReliablyInsertMessage(MessageForDelivery message, DbContext context) { context.Database.ExecuteSqlCommand(this.insertQuery, new SqlParameter("@Body", message.Body), new SqlParameter("@DeliveryDate", message.DeliveryDate.HasValue ? (object)message.DeliveryDate.Value : DBNull.Value), new SqlParameter("@CorrelationId", (object)message.CorrelationId ?? DBNull.Value)); }
/// <summary> /// Sends the specified message. /// </summary> public void Send(MessageForDelivery message) { using (var connection = this.connectionFactory.CreateConnection(this.connectionString)) { connection.Open(); this.InsertMessage(message, connection); } }
private void InsertMessage(MessageForDelivery message, DbConnection connection) { using (var command = (SqlCommand)connection.CreateCommand()) { command.CommandText = this.insertQuery; command.CommandType = CommandType.Text; command.Parameters.Add("@Body", SqlDbType.NVarChar).Value = message.Body; command.Parameters.Add("@DeliveryDate", SqlDbType.DateTime).Value = message.DeliveryDate.HasValue ? (object)message.DeliveryDate.Value : DBNull.Value; command.Parameters.Add("@CorrelationId", SqlDbType.NVarChar).Value = (object)message.CorrelationId ?? DBNull.Value; command.ExecuteNonQuery(); } }
public MessageReceivedEventArgs(MessageForDelivery message) { this.Message = message; }
protected bool ReceiveMessage(DbConnection connection, DbTransaction transaction) { var currentDate = this.GetCurrentDate(); long messageId = -1; MessageForDelivery message = null; try { using (var command = connection.CreateCommand()) { command.Transaction = transaction; command.CommandType = CommandType.Text; command.CommandText = this.readQuery; ((SqlCommand)command).Parameters.Add("@CurrentDate", SqlDbType.DateTime).Value = currentDate; using (var reader = command.ExecuteReader()) { if (!reader.Read()) { return(false); } // Delegate message receiving to another process as fast as possible this.delegateMessageReceiving.Invoke(); var body = (string)reader["Body"]; var deliveryDateValue = reader["DeliveryDate"]; var deliveryDate = deliveryDateValue == DBNull.Value ? (DateTime?)null : new DateTime?((DateTime)deliveryDateValue); var correlationIdValue = reader["CorrelationId"]; var correlationId = (string)(correlationIdValue == DBNull.Value ? null : correlationIdValue); message = new MessageForDelivery(body, correlationId, deliveryDate); messageId = (long)reader["Id"]; } } this.MessageReceived(this, new MessageReceivedEventArgs(message)); using (var command = connection.CreateCommand()) { command.Transaction = transaction; command.CommandType = CommandType.Text; command.CommandText = this.deleteQuery; ((SqlCommand)command).Parameters.Add("@Id", SqlDbType.BigInt).Value = messageId; command.ExecuteNonQuery(); } transaction.Commit(); } catch (Exception e) { try { // Dead Lettering using (var command = connection.CreateCommand()) { command.Transaction = transaction; command.CommandType = CommandType.Text; command.CommandText = this.setDeadLetterQuery; ((SqlCommand)command).Parameters.Add("@Id", SqlDbType.BigInt).Value = messageId; ((SqlCommand)command).Parameters.Add("@TraceInfo", SqlDbType.NVarChar).Value = string.Format("Exception Type: {0}. Exception Message: {1} Inner Exception Type: {2}. Inner Exception Message: {3} StackTrace: {4}", e.GetType().Name, e.Message, e.InnerException.GetType(), e.InnerException.Message, e.StackTrace); command.ExecuteNonQuery(); } transaction.Commit(); } catch (Exception) { try { transaction.Rollback(); } catch (Exception) { // NOTE: we catch ANY exceptions. This implementation // supports retries and dead-lettering. } } } return(true); }