示例#1
0
        public bool Send(string queueName, string routingKey, MessageDto message)
        {
            try
            {
                lock (_factory)
                {
                    using (_connection = _factory.CreateConnection())
                    {
                        using (_model = _connection.CreateModel())
                        {
                            _model.ExchangeDeclare(_rabbitMQSettings.ExchangeName, _rabbitMQSettings.Type, durable: true);

                            _model.QueueDeclare(queueName, true, false, false, null);
                            _model.QueueBind(queueName, _rabbitMQSettings.ExchangeName, routingKey);

                            message.InstanceId = Me.GetInstance().Id;
                            message.RequestId  = Guid.NewGuid();
                            message.CreateAt   = DomainUtils.GetLocalDate();

                            byte[] objsend = ObjectSerialize.Serialize(message);

                            _model.BasicPublish(_rabbitMQSettings.ExchangeName, routingKey, null, objsend);
                        }
                    }
                }

                using (var scope = Services.CreateScope())
                {
                    var scopedProcessingService = scope.ServiceProvider.GetRequiredService <IMessageService>();
                    scopedProcessingService.InsertAsync(message).Wait();
                }

                return(true);
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
                _model.Dispose();
                _connection.Dispose();
                return(false);
            }
        }