Пример #1
0
        protected virtual string PublishMessage(
            string routing,
            byte[] messageBody,
            string correlationId = null,
            Dictionary <string, string> messageHeaders = null)
        {
            if (string.IsNullOrWhiteSpace(correlationId))
            {
                correlationId = Guid.NewGuid().ToString("N");
            }

            if (this._options.BusType == MessageClientOptions.MessageBusTypeEnum.QUEUE)
            {
                channel.QueueDeclare(
                    //queue: routing,
                    queue: this._options.QueueName,
                    durable: this._options.QueueDurable,
                    exclusive: false,
                    autoDelete: false,
                    arguments: null);
            }
            else if (this._options.BusType == MessageClientOptions.MessageBusTypeEnum.EXCHANGE)
            {
                channel.ExchangeDeclare(
                    this._options.ExchangeName,
                    this._options.ExchangeType,
                    true,
                    false,
                    null);
            }
            else
            {
                throw new InvalidOperationException();
            }

            var trackHeaders = new Dictionary <string, string>();

            if (TrackContext.TryToHeaders(this._services, trackHeaders))
            {
            }

            IBasicProperties props = null;//this.PrepareMessageProperties(correlationId, messageHeaders);

            {
                props = channel.CreateBasicProperties();

                props.ContentType = "application/json";
                if (this._options.MessageExpirationTimeout != null)
                {
                    props.Expiration = this._options.MessageExpirationTimeout.Value.TotalMilliseconds.ToString();
                }

                props.Headers = new Dictionary <string, object>();
                if (messageHeaders != null)
                {
                    foreach (string key in messageHeaders.Keys)
                    {
                        props.Headers[key] = Encoding.UTF8.GetBytes(messageHeaders[key] ?? "");
                    }
                }
                if (trackHeaders != null)
                {
                    foreach (string key in trackHeaders.Keys)
                    {
                        props.Headers[key] = Encoding.UTF8.GetBytes(trackHeaders[key] ?? "");
                    }
                }

                //if (this._services != null)
                //{
                //    var track = this._services.GetService<TrackContext>();
                //    if (track != null)
                //    {
                //        props.Headers["x-shop-id"] = track.ShopId.ToString();
                //        props.Headers["x-member-id"] = track.MemberId;
                //        props.Headers["x-session-id"] = track.SessionId;
                //        props.Headers["x-channel-id"] = track.ChannelId;
                //        props.Headers["x-request-id"] = track.RequestId;
                //    }
                //}

                props.CorrelationId = correlationId; //Guid.NewGuid().ToString("N");

                this.SetupMessageProperties(props);
            }

            if (this._options.BusType == MessageClientOptions.MessageBusTypeEnum.EXCHANGE)
            {
                channel.BasicPublish(
                    exchange: this._options.ExchangeName ?? "",
                    routingKey: routing,
                    basicProperties: props,
                    body: messageBody); //Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)));
            }
            else if (this._options.BusType == MessageClientOptions.MessageBusTypeEnum.QUEUE)
            {
                channel.BasicPublish(
                    exchange: "",
                    routingKey: this._options.QueueName,
                    basicProperties: props,
                    body: messageBody); //Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)));
            }

            return(correlationId);
        }