public void Send(ISendContext context)
        {
            AddProducerBinding();

            _connectionHandler.Use(connection =>
            {
                try
                {
                    IBasicProperties properties = _producer.CreateProperties();

                    properties.SetPersistent(context.DeliveryMode == DeliveryMode.Persistent);
                    properties.MessageId = context.MessageId ?? properties.MessageId ?? NewId.Next().ToString();
                    if (context.ExpirationTime.HasValue)
                    {
                        DateTime value        = context.ExpirationTime.Value;
                        properties.Expiration =
                            (value.Kind == DateTimeKind.Utc
                                     ? value - SystemUtil.UtcNow
                                     : value - SystemUtil.Now).
                            TotalMilliseconds.ToString("F0", CultureInfo.InvariantCulture);
                    }

                    using (var body = new MemoryStream())
                    {
                        context.SerializeTo(body);
                        properties.Headers = context.Headers.ToDictionary(entry => entry.Key, entry => (object)entry.Value);
                        properties.Headers["Content-Type"] = context.ContentType;

#if NET40
                        var task = _producer.PublishAsync(_address.Name, properties, body.ToArray());
                        task.Wait();
#else
                        _producer.Publish(_address.Name, properties, body.ToArray());
#endif

                        _address.LogSent(context.MessageId ?? properties.MessageId ?? "", context.MessageType);
                    }
                }
#if NET40
                catch (AggregateException ex)
                {
                    throw new TransportException(_address.Uri, "Publisher did not confirm message", ex.InnerException);
                }
#endif
                catch (AlreadyClosedException ex)
                {
                    throw new InvalidConnectionException(_address.Uri, "Connection was already closed", ex);
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidConnectionException(_address.Uri, "Connection was closed", ex);
                }
                catch (OperationInterruptedException ex)
                {
                    throw new InvalidConnectionException(_address.Uri, "Operation was interrupted", ex);
                }
            });
        }
예제 #2
0
        public void Send(ISendContext context)
        {
            AddProducerBinding();

            _connectionHandler.Use(connection =>
            {
                try
                {
                    IBasicProperties properties = _producer.CreateProperties();

                    properties.SetPersistent(true);
                    properties.MessageId = context.MessageId ?? properties.MessageId ?? NewId.Next().ToString();
                    if (context.ExpirationTime.HasValue)
                    {
                        DateTime value        = context.ExpirationTime.Value;
                        properties.Expiration =
                            (value.Kind == DateTimeKind.Utc
                                     ? value - SystemUtil.UtcNow
                                     : value - SystemUtil.Now).
                            TotalMilliseconds.ToString();
                    }

                    using (var body = new MemoryStream())
                    {
                        context.SerializeTo(body);
                        properties.Headers = new Hashtable {
                            { "Content-Type", context.ContentType }
                        };

                        _producer.Publish(_address.Name, properties, body.ToArray());

                        _address.LogSent(context.MessageId ?? "", context.MessageType);
                    }
                }
                catch (AlreadyClosedException ex)
                {
                    throw new InvalidConnectionException(_address.Uri, "Connection was already closed", ex);
                }
                catch (EndOfStreamException ex)
                {
                    throw new InvalidConnectionException(_address.Uri, "Connection was closed", ex);
                }
                catch (OperationInterruptedException ex)
                {
                    throw new InvalidConnectionException(_address.Uri, "Operation was interrupted", ex);
                }
            });
        }