Example #1
0
        public override async Task <bool> Send(Guid messageId, string from, XmlReader input)
        {
            var reader = await Transform(messageId, adress.Host, from, xsltFile, input);

            var xml = new SqlXml(reader);

            if (!xml.IsNull)
            {
                using (var connection = await sqlConnectionFactory.OpenNewConnection().ConfigureAwait(false))
                    using (var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted))
                    {
                        var commmand = new SqlCommand(procedureName, connection, transaction);
                        commmand.CommandType = CommandType.StoredProcedure;
                        commmand.Parameters.AddWithValue("@MessageId", messageId);
                        commmand.Parameters.AddWithValue("@From", from);
                        commmand.Parameters.AddWithValue("@Xml", xml);

                        await commmand.ExecuteNonQueryAsync().ConfigureAwait(false);

                        transaction.Commit();
                    }
            }

            reader.Close();
            return(true);
        }
Example #2
0
        async Task ReceiveMessage(CancellationTokenSource receiveCancellationTokenSource)
        {
            Message message = null;

            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions {
                    IsolationLevel = IsolationLevel.ReadCommitted, Timeout = TimeSpan.FromSeconds(30)
                }, TransactionScopeAsyncFlowOption.Enabled))
                    using (var connection = await sqlConnectionFactory.OpenNewConnection().ConfigureAwait(false))
                    {
                        message = await inputQueue.TryReceive(connection, null, receiveCancellationTokenSource).ConfigureAwait(false);

                        if (message == null)
                        {
                            // The message was received but is not fit for processing (e.g. was DLQd).
                            // In such a case we still need to commit the transport tx to remove message
                            // from the queue table.
                            scope.Complete();
                            return;
                        }

                        connection.Close();

                        if (!await TryProcess(message, scope).ConfigureAwait(false))
                        {
                            return;
                        }

                        scope.Complete();

                        failureInfoStorage.ClearFailureInfoForMessage(message.MessageId.ToString());
                    }
            }
            catch (Exception exception)
            {
                if (message == null)
                {
                    throw;
                }

                failureInfoStorage.RecordFailureInfoForMessage(message.MessageId.ToString(), exception);
            }
        }
Example #3
0
        public async Task Receive(CancellationToken ct)
        {
            Trace.TraceInformation($"{Name}:{tableName} started");
            await Task.Delay(TimeSpan.FromSeconds(5)).ConfigureAwait(false);

            while (!ct.IsCancellationRequested)
            {
                //var stopwatch = Stopwatch.StartNew();
                Envelope envelope = null;

                using (var connection = await connectionFactory.OpenNewConnection().ConfigureAwait(false))
                    using (var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted))
                    {
                        envelope = await TryReceive(connection, transaction, ct).ConfigureAwait(false);

                        if (envelope == null)
                        {
                            Trace.TraceInformation($"{Name}:{tableName} Not received message");
                            transaction.Commit();
                            return;
                        }

                        Trace.TraceInformation($"{Name}:{tableName} Received message");

                        try
                        {
                            await TryProcess(envelope).ConfigureAwait(false);

                            Trace.TraceInformation($"{Name}:{tableName} Processed message");
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError($"{Name}:{tableName} Error : {ex.Message}");
                            await Rollback(connection, transaction, ct, envelope, ex).ConfigureAwait(false);
                        }

                        transaction.Commit();
                    }
            }
        }
Example #4
0
        async Task Processor(CancellationToken cancellationToken)
        {
            using (var connection = await sqlConnectionFactory.OpenNewConnection().ConfigureAwait(false))
            {
                var command    = new SqlCommand("SELECT [Table], [Concurrency] FROM [Queue]", connection);
                var dataReader = await command.ExecuteReaderAsync().ConfigureAwait(false);

                while (await TryReadQueue(dataReader, cancellationToken).ConfigureAwait(false) && !cancellationToken.IsCancellationRequested)
                {
                }
                ;
            }
        }