Пример #1
0
        public void ReceiveMessage(SqlTransaction transaction)
        {
            TransportMessage message = null;
            Guid id;
            try
            {
                using (var cmd = transaction.Connection.CreateCommand())
                {
                    cmd.Transaction = transaction;
                    cmd.CommandText = string.Format(SqlCommands.SelectMessage, ListenerQueue);
                    var backoff = new BackOff(1000);
                    while (message == null)
                    {
                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                message = new TransportMessage();
                                id = reader.GetGuid(0);
                                message.Id = id.ToString();
                                message.CorrelationId = reader.GetString(1) ?? string.Empty;
                                message.ReturnAddress = reader.GetString(2);
                                message.MessageIntent = (MessageIntentEnum)reader.GetByte(3);
                                message.HeadersString = reader.GetString(4) ?? string.Empty;
                                message.BodyString = reader.GetString(5) ?? string.Empty;
                                try
                                {
                                    using (var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(message.BodyString)))
                                    {
                                        message.Body = MessageSerializer.Deserialize(bodyStream);
                                    }
                                    if (message.HeadersString.Length > 0)
                                    {
                                        using (var headersStream = new MemoryStream(Encoding.UTF8.GetBytes(message.HeadersString)))
                                        {
                                            message.Headers = MessageSerializer.Deserialize(headersStream).Cast<HeaderInfo>().ToList();
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Logger.Error("Could not extract message data.", ex);
                                    OnSerializationFailed(id, message, message.BodyString, message.HeadersString, ex);
                                    return; // deserialization failed - no reason to try again, so don't throw
                                }
                            }
                        }
                        if (message == null)
                        {
                            backoff.Wait(() => true);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error in receiving message from queue.", e);
                throw; // Throw to rollback 
            }
            finally
            {
                transactionWaitPool.Release(1);
                releasedWaitLock = true;
            }

            // No message? That's okay
            if (message == null)
            {
                return;
            }

            // Set the correlation Id
            if (string.IsNullOrEmpty(message.IdForCorrelation))
            {
                message.IdForCorrelation = message.Id;
            }
            ProcessMessage(message);
        }
Пример #2
0
        public void ReceiveMessage(SqlTransaction transaction)
        {
            TransportMessage message = null;
            Guid             id;

            try
            {
                using (var cmd = transaction.Connection.CreateCommand())
                {
                    cmd.Transaction = transaction;
                    cmd.CommandText = string.Format(SqlCommands.SelectMessage, ListenerQueue);
                    var backoff = new BackOff(1000);
                    while (message == null)
                    {
                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                message               = new TransportMessage();
                                id                    = reader.GetGuid(0);
                                message.Id            = id.ToString();
                                message.CorrelationId = reader.GetString(1) ?? string.Empty;
                                message.ReturnAddress = reader.GetString(2);
                                message.MessageIntent = (MessageIntentEnum)reader.GetByte(3);
                                message.HeadersString = reader.GetString(4) ?? string.Empty;
                                message.BodyString    = reader.GetString(5) ?? string.Empty;
                                try
                                {
                                    using (var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(message.BodyString)))
                                    {
                                        message.Body = MessageSerializer.Deserialize(bodyStream);
                                    }
                                    if (message.HeadersString.Length > 0)
                                    {
                                        using (var headersStream = new MemoryStream(Encoding.UTF8.GetBytes(message.HeadersString)))
                                        {
                                            message.Headers = MessageSerializer.Deserialize(headersStream).Cast <HeaderInfo>().ToList();
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Logger.Error("Could not extract message data.", ex);
                                    OnSerializationFailed(id, message, message.BodyString, message.HeadersString, ex);
                                    return; // deserialization failed - no reason to try again, so don't throw
                                }
                            }
                        }
                        if (message == null)
                        {
                            backoff.Wait(() => true);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error in receiving message from queue.", e);
                throw; // Throw to rollback
            }
            finally
            {
                transactionWaitPool.Release(1);
                releasedWaitLock = true;
            }

            // No message? That's okay
            if (message == null)
            {
                return;
            }

            // Set the correlation Id
            if (string.IsNullOrEmpty(message.IdForCorrelation))
            {
                message.IdForCorrelation = message.Id;
            }
            ProcessMessage(message);
        }