Esempio n. 1
0
        public Task Publish(IMessage message, CancellationToken cancellationToken)
        {
            var eventType    = message.GetType();
            var exchangeName = eventType.Namespace.Split('.').First(); //TODO: exchangeName
            var routingKey   = eventType.Name;
            var queueName    = $"{exchangeName}.{routingKey}";

            _publishChannel.Value.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Direct);
            _publishChannel.Value.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false);
            _publishChannel.Value.QueueBind(queue: queueName, exchange: exchangeName, routingKey: routingKey);

            return(_taskFactory.StartNew(() =>
            {
                var packedMessage = (string)_messagePacker.Pack(message, cancellationToken).GetAwaiter().GetResult();
                _publishChannel.Value.BasicPublish(exchange: exchangeName, routingKey: routingKey, body: Encoding.UTF8.GetBytes(packedMessage));
            }, cancellationToken));
        }
Esempio n. 2
0
        protected virtual string Serialize <T>(T obj, string callbackName = null)
        {
            string content;

            if (obj != null)
            {
                content = Helper.IsComplexType(obj.GetType())
                    ? _serializer.Serialize(obj)
                    : obj.ToString();
            }
            else
            {
                content = string.Empty;
            }
            var message = new CapMessageDto(content)
            {
                CallbackName = callbackName
            };

            return(_msgPacker.Pack(message));
        }
        public async Task SendAsync(string messageId, string topicName, object bodyObj)
        {
            string body;

            if (bodyObj != null && Helper.IsComplexType(bodyObj.GetType()))
            {
                body = _contentSerializer.Serialize(bodyObj);
            }
            else
            {
                body = bodyObj?.ToString();
            }

            _logger.LogDebug($"Callback message will publishing, name:{topicName},content:{body}");

            var callbackMessage = new CapMessageDto
            {
                Id      = messageId,
                Content = body
            };

            var content = _messagePacker.Pack(callbackMessage);

            var publishedMessage = new CapPublishedMessage
            {
                Id         = SnowflakeId.Default().NextId(),
                Name       = topicName,
                Content    = content,
                StatusName = StatusName.Scheduled
            };

            using (var scope = _serviceProvider.CreateScope())
            {
                var provider          = scope.ServiceProvider;
                var callbackPublisher = provider.GetService <ICallbackPublisher>();
                await callbackPublisher.PublishCallbackAsync(publishedMessage);
            }
        }