Exemple #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var rabbitConfig = new RabbitMQEntityConfiguration();

            new ConfigureFromConfigurationOptions <RabbitMQEntityConfiguration>(
                Configuration.GetSection("RabbitMQEntityConfiguration"))
            .Configure(rabbitConfig);

            services.AddSingleton(rabbitConfig);

            services.AddControllers();
        }
Exemple #2
0
        public object Post(
            [FromServices] RabbitMQEntityConfiguration configurations,
            [FromBody] Content content)
        {
            lock (_Conter)
            {
                _Conter.Increment();

                var factory = new ConnectionFactory()
                {
                    HostName = configurations.HostName,
                    Port     = configurations.Port,
                    UserName = configurations.UserName,
                    Password = configurations.Password
                };

                using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        channel.QueueDeclare(queue: "TestAspCore",
                                             durable: false,
                                             exclusive: false,
                                             autoDelete: false,
                                             arguments: null);

                        string message =
                            $"{DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")} - " +
                            $"Message content: {content.Message}";

                        var body = Encoding.UTF8.GetBytes(message);

                        channel.BasicPublish(exchange: "",
                                             routingKey: "TestAspCore",
                                             basicProperties: null,
                                             body: body);
                    }

                return(new
                {
                    Result = "Message sent with sucess"
                });
            }
        }