public async Task <IEnumerable <IClientOutboxMessageAwaitingDispatch> > GetAwaitingDispatchAsync()
        {
            using (var connection = await _connectionBuilder.OpenConnectionAsync().ConfigureAwait(false))
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = GetAwaitingDispatchCommandText;
                    command.CommandType = CommandType.Text;
                    command.AddParameter("CreatedAt", _dateTimeService.UtcNow.AddMinutes(-10));

                    using (var reader = await command.ExecuteReaderAsync().ConfigureAwait(false))
                    {
                        var clientOutboxMessages = new List <IClientOutboxMessageAwaitingDispatch>();

                        while (await reader.ReadAsync().ConfigureAwait(false))
                        {
                            var messageId           = reader.GetGuid(0);
                            var endpointName        = reader.GetString(1);
                            var clientOutboxMessage = new ClientOutboxMessage(messageId, endpointName);

                            clientOutboxMessages.Add(clientOutboxMessage);
                        }

                        return(clientOutboxMessages);
                    }
                }
        }
        public async Task <ClientOutboxMessage> GetAsync(Guid messageId, SynchronizedStorageSession synchronizedStorageSession)
        {
            var sqlStorageSession = synchronizedStorageSession.GetSqlStorageSession();

            using (var command = sqlStorageSession.Connection.CreateCommand())
            {
                command.CommandText = GetCommandText;
                command.CommandType = CommandType.Text;
                command.Transaction = sqlStorageSession.Transaction;
                command.AddParameter("MessageId", messageId);

                using (var reader = await command.ExecuteReaderAsync(CommandBehavior.SingleRow).ConfigureAwait(false))
                {
                    if (await reader.ReadAsync().ConfigureAwait(false))
                    {
                        var endpointName = reader.GetString(0);
                        var operations   = JsonConvert.DeserializeObject <List <object> >(reader.GetString(1), new JsonSerializerSettings {
                            TypeNameHandling = TypeNameHandling.Auto
                        });
                        var clientOutboxMessage = new ClientOutboxMessage(messageId, endpointName, operations);

                        return(clientOutboxMessage);
                    }

                    throw new KeyNotFoundException($"Client outbox data not found where MessageId = '{messageId}'");
                }
            }
        }
        public ProcessClientOutboxMessageCommandHandlerTestsFixture()
        {
            Now          = DateTime.UtcNow;
            EndpointName = "SFA.DAS.NServiceBus";

            Events = new List <object>
            {
                new FooEvent(Now.AddDays(-1)),
                new BarEvent(Now)
            };

            ClientOutboxMessage        = new ClientOutboxMessage(GuidComb.NewGuidComb(), EndpointName, Events);
            SynchronizedStorageSession = new Mock <SynchronizedStorageSession>();

            Context = new TestableMessageHandlerContext
            {
                MessageId = ClientOutboxMessage.MessageId.ToString(),
                SynchronizedStorageSession = SynchronizedStorageSession.Object
            };

            Command             = new ProcessClientOutboxMessageCommand();
            ClientOutboxStorage = new Mock <IClientOutboxStorage>();

            ClientOutboxStorage.Setup(o => o.GetAsync(ClientOutboxMessage.MessageId, SynchronizedStorageSession.Object)).ReturnsAsync(ClientOutboxMessage);

            Handler = new ProcessClientOutboxMessageCommandHandler(ClientOutboxStorage.Object);
        }
        public ClientOutboxPersisterTestsFixture()
        {
            Now             = DateTime.UtcNow;
            DateTimeService = new Mock <IDateTimeService>();
            Settings        = new Mock <ReadOnlySettings>();
            Connection      = new Mock <DbConnection>();
            Transaction     = new Mock <DbTransaction> {
                CallBase = true
            };
            Command    = new Mock <DbCommand>();
            Parameters = new Mock <DbParameterCollection>();
            ClientOutboxTransaction = new SqlClientOutboxTransaction(Connection.Object, Transaction.Object);
            EndpointName            = "SFA.DAS.NServiceBus";

            Events = new List <object>
            {
                new FooEvent(Now.AddDays(-1)),
                new BarEvent(Now)
            };

            EventsData = JsonConvert.SerializeObject(Events, new JsonSerializerSettings {
                TypeNameHandling = TypeNameHandling.Auto
            });
            ClientOutboxMessage        = new ClientOutboxMessage(GuidComb.NewGuidComb(), EndpointName, Events);
            SynchronizedStorageSession = new Mock <SynchronizedStorageSession>();
            SqlSession              = SynchronizedStorageSession.As <ISqlStorageSession>();
            OutboxMessages          = new List <IClientOutboxMessageAwaitingDispatch>();
            CancellationTokenSource = new CancellationTokenSource();
            CancellationToken       = CancellationTokenSource.Token;

            DateTimeService.Setup(d => d.UtcNow).Returns(Now);
            Parameters.Setup(p => p.Add(It.IsAny <DbParameter>()));
            Command.SetupSet(c => c.CommandText = It.IsAny <string>());
            Command.SetupSet(c => c.Transaction = It.IsAny <DbTransaction>());

            Command.Protected().Setup <DbParameter>("CreateDbParameter").Returns(() =>
            {
                var parameter = new Mock <DbParameter> {
                    CallBase = true
                };

                parameter.SetupProperty(p => p.ParameterName);
                parameter.SetupProperty(p => p.Value);

                return(parameter.Object);
            });

            Command.Protected().Setup <DbParameterCollection>("DbParameterCollection").Returns(Parameters.Object);
            Connection.Protected().Setup <DbTransaction>("BeginDbTransaction", IsolationLevel.Unspecified).Returns(Transaction.Object);
            Connection.Protected().Setup <DbCommand>("CreateDbCommand").Returns(Command.Object);
            Settings.Setup(s => s.Get <Func <DbConnection> >("SqlPersistence.ConnectionBuilder")).Returns(() => Connection.Object);
            SqlSession.Setup(s => s.Connection).Returns(Connection.Object);
            SqlSession.Setup(s => s.Transaction).Returns(Transaction.Object);

            ClientOutboxStorage = new ClientOutboxPersister(DateTimeService.Object, Settings.Object);
        }
        public Task StoreAsync(ClientOutboxMessage clientOutboxMessage, IClientOutboxTransaction clientOutboxTransaction)
        {
            var sqlClientOutboxTransaction = (SqlClientOutboxTransaction)clientOutboxTransaction;

            using (var command = sqlClientOutboxTransaction.Connection.CreateCommand())
            {
                command.CommandText = StoreCommandText;
                command.CommandType = CommandType.Text;
                command.Transaction = sqlClientOutboxTransaction.Transaction;
                command.AddParameter("MessageId", clientOutboxMessage.MessageId);
                command.AddParameter("EndpointName", clientOutboxMessage.EndpointName);
                command.AddParameter("CreatedAt", _dateTimeService.UtcNow);
                command.AddParameter("Operations", JsonConvert.SerializeObject(clientOutboxMessage.Operations, new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.Auto
                }));

                return(command.ExecuteNonQueryAsync());
            }
        }