Ejemplo n.º 1
0
    static async Task Main()
    {
        Console.Title = "Samples.SqlServer.Native.ErrorProcessor";
        Console.WriteLine("Press any key to exit");

        await CreateErrorQueue().ConfigureAwait(false);

        #region MessageProcessingLoop

        long startingRow;

        var rowVersionTracker = new RowVersionTracker();
        using (var connection = await ConnectionHelpers.OpenConnection(SqlHelper.ConnectionString)
                                .ConfigureAwait(false))
        {
            await rowVersionTracker.CreateTable(connection).ConfigureAwait(false);

            startingRow = await rowVersionTracker.Get(connection).ConfigureAwait(false);
        }

        Task Callback(SqlTransaction sqlTransaction, IncomingBytesMessage message, CancellationToken cancellation)
        {
            var bodyText = Encoding.UTF8.GetString(message.Body);

            Console.WriteLine($"Message received in error queue:\r\n{bodyText}");
            return(Task.CompletedTask);
        }

        void ErrorCallback(Exception exception)
        {
            Environment.FailFast("Message processing loop failed", exception);
        }

        Task PersistRowVersion(SqlTransaction transaction, long rowVersion, CancellationToken cancellation)
        {
            return(rowVersionTracker.Save(transaction, rowVersion, cancellation));
        }

        Task <SqlTransaction> TransactionBuilder(CancellationToken cancellation)
        {
            return(ConnectionHelpers.BeginTransaction(SqlHelper.ConnectionString, cancellation));
        }

        var processingLoop = new MessageProcessingLoop(
            table: "error",
            delay: TimeSpan.FromSeconds(1),
            transactionBuilder: TransactionBuilder,
            callback: Callback,
            errorCallback: ErrorCallback,
            startingRow: startingRow,
            persistRowVersion: PersistRowVersion);
        processingLoop.Start();

        Console.ReadKey();

        await processingLoop.Stop()
        .ConfigureAwait(false);

        #endregion
    }
    async Task ReadLoop()
    {
        #region ProcessingLoop

        var rowVersionTracker = new RowVersionTracker();

        var startingRow = await rowVersionTracker.Get(sqlConnection)
                          .ConfigureAwait(false);

        async Task Callback(SqlTransaction transaction, IncomingMessage message, CancellationToken cancellation)
        {
            using (var reader = new StreamReader(message.Body))
            {
                var bodyText = await reader.ReadToEndAsync()
                               .ConfigureAwait(false);

                Console.WriteLine($"Message received in error message:\r\n{bodyText}");
            }
        }

        void ErrorCallback(Exception exception)
        {
            Environment.FailFast("Message processing loop failed", exception);
        }

        Task <SqlTransaction> TransactionBuilder(CancellationToken cancellation)
        {
            return(ConnectionHelpers.BeginTransaction(connectionString, cancellation));
        }

        Task PersistRowVersion(SqlTransaction transaction, long rowVersion, CancellationToken token)
        {
            return(rowVersionTracker.Save(sqlConnection, rowVersion, token));
        }

        var processingLoop = new MessageProcessingLoop(
            table: "error",
            delay: TimeSpan.FromSeconds(1),
            transactionBuilder: TransactionBuilder,
            callback: Callback,
            errorCallback: ErrorCallback,
            startingRow: startingRow,
            persistRowVersion: PersistRowVersion);
        processingLoop.Start();

        Console.ReadKey();

        await processingLoop.Stop()
        .ConfigureAwait(false);

        #endregion
    }
Ejemplo n.º 3
0
    static async Task Main()
    {
        Console.Title = "Samples.SqlServer.Native.AuditConsumer";
        Console.WriteLine("Press any key to exit");

        await CreateAuditQueue().ConfigureAwait(false);

        #region MessageConsumingLoop

        async Task Callback(SqlTransaction transaction, IncomingMessage message, CancellationToken cancellation)
        {
            using (var reader = new StreamReader(message.Body))
            {
                var bodyText = await reader.ReadToEndAsync().ConfigureAwait(false);

                Console.WriteLine($"Message received in audit queue:\r\n{bodyText}");
            }
        }

        void ErrorCallback(Exception exception)
        {
            Environment.FailFast("Message consuming loop failed", exception);
        }

        Task <SqlTransaction> TransactionBuilder(CancellationToken cancellation)
        {
            return(ConnectionHelpers.BeginTransaction(SqlHelper.ConnectionString, cancellation));
        }

        var consumingLoop = new MessageConsumingLoop(
            table: "audit",
            delay: TimeSpan.FromSeconds(1),
            transactionBuilder: TransactionBuilder,
            callback: Callback,
            errorCallback: ErrorCallback);
        consumingLoop.Start();

        Console.ReadKey();

        await consumingLoop.Stop()
        .ConfigureAwait(false);

        #endregion
    }
    async Task ConsumeLoop()
    {
        #region ConsumeLoop

        async Task Callback(SqlTransaction transaction, IncomingMessage message, CancellationToken cancellation)
        {
            using (var reader = new StreamReader(message.Body))
            {
                var bodyText = await reader.ReadToEndAsync()
                               .ConfigureAwait(false);

                Console.WriteLine($"Reply received:\r\n{bodyText}");
            }
        }

        Task <SqlTransaction> TransactionBuilder(CancellationToken cancellation)
        {
            return(ConnectionHelpers.BeginTransaction(connectionString, cancellation));
        }

        void ErrorCallback(Exception exception)
        {
            Environment.FailFast("Message consuming loop failed", exception);
        }

        // start consuming
        var consumingLoop = new MessageConsumingLoop(
            table: "endpointTable",
            delay: TimeSpan.FromSeconds(1),
            transactionBuilder: TransactionBuilder,
            callback: Callback,
            errorCallback: ErrorCallback);
        consumingLoop.Start();

        // stop consuming
        await consumingLoop.Stop()
        .ConfigureAwait(false);

        #endregion
    }
Ejemplo n.º 5
0
    static async Task Main()
    {
        Console.Title = "Samples.SqlServer.Native.NativeEndpoint";
        Console.WriteLine("Press s to send a message that will succeed");
        Console.WriteLine("Press f to send a message that will fail");
        Console.WriteLine("Press any key to exit");

        await CreateQueue().ConfigureAwait(false);

        #region receive

        Task Callback(SqlTransaction sqlTransaction, IncomingBytesMessage message, CancellationToken cancellation)
        {
            var bodyText = Encoding.UTF8.GetString(message.Body);

            Console.WriteLine($"Reply received:\r\n{bodyText}");
            return(Task.CompletedTask);
        }

        void ErrorCallback(Exception exception)
        {
            Environment.FailFast("Message consuming loop failed", exception);
        }

        Task <SqlTransaction> TransactionBuilder(CancellationToken cancellation)
        {
            return(ConnectionHelpers.BeginTransaction(SqlHelper.ConnectionString, cancellation));
        }

        var messageConsumingLoop = new MessageConsumingLoop(
            table: "NativeEndpoint",
            delay: TimeSpan.FromSeconds(1),
            transactionBuilder: TransactionBuilder,
            callback: Callback,
            errorCallback: ErrorCallback);
        messageConsumingLoop.Start();

        #endregion

        while (true)
        {
            var key = Console.ReadKey();
            Console.WriteLine();

            if (key.Key == ConsoleKey.S)
            {
                await SendMessageThatWillSucceed()
                .ConfigureAwait(false);

                continue;
            }

            if (key.Key == ConsoleKey.F)
            {
                await SendMessageThatWillFail()
                .ConfigureAwait(false);

                continue;
            }

            break;
        }

        await messageConsumingLoop.Stop()
        .ConfigureAwait(false);
    }
Ejemplo n.º 6
0
 Task <DbTransaction> TransactionBuilder(CancellationToken cancellation)
 {
     return(ConnectionHelpers.BeginTransaction(connectionString, cancellation));
 }
 Task <SqlTransaction> BuildTransaction(CancellationToken cancellation)
 {
     return(ConnectionHelpers.BeginTransaction(connectionString, cancellation));
 }