public async Task Consume(ConsumeContext <StatesRequest> context)
        {
            Console.Out.WriteLine($"Received: StatesRequest");

            await Console.Out.WriteLineAsync("States response sent");

            Configuration configuration = new Configuration();

            using (StreamReader r = new StreamReader("configuration.json"))
            {
                string json = r.ReadToEnd();
                configuration = JsonConvert.DeserializeObject <Configuration>(json);
            }

            var connectionString = "Server=localhost;Database=Locations;User ID=sa;Password=Ariel123!@;";

            var DB_NAME = Environment.GetEnvironmentVariable("DB_NAME") ?? configuration.Database;
            var DB_HOST = Environment.GetEnvironmentVariable("DB_HOST") ?? configuration.DbHost;

            connectionString = connectionString.Replace(configuration.DbHost, DB_HOST);
            connectionString = connectionString.Replace(configuration.Database, DB_NAME);

            var optionsBuilder = new DbContextOptionsBuilder <LocationsDbContext>();

            optionsBuilder.UseSqlServer(connectionString);

            var states = new LocationsDbContext(optionsBuilder.Options).States.ToList();

            var statesResponse = new StateResponse();

            foreach (var state in states)
            {
                statesResponse.States.Add(new Contracts.State
                {
                    Id   = state.Id,
                    Name = state.Name
                });
            }

            await context.RespondAsync(statesResponse);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Configuration configuration = new Configuration();

            using (StreamReader r = new StreamReader("configuration.json"))
            {
                string json = r.ReadToEnd();
                configuration = JsonConvert.DeserializeObject <Configuration>(json);
            }

            Console.Out.WriteLine("Configuring service bus");

            var connectionString = "Server=localhost;Database=Locations;User ID=sa;Password=Ariel123!@;";

            var DB_NAME = Environment.GetEnvironmentVariable("DB_NAME") ?? configuration.Database;
            var DB_HOST = Environment.GetEnvironmentVariable("DB_HOST") ?? configuration.DbHost;

            connectionString = connectionString.Replace(configuration.DbHost, DB_HOST);
            connectionString = connectionString.Replace(configuration.Database, DB_NAME);

            var optionsBuilder = new DbContextOptionsBuilder <LocationsDbContext>();

            optionsBuilder.UseSqlServer(connectionString);

            var dbContext = new LocationsDbContext(optionsBuilder.Options);

            while (true)
            {
                try
                {
                    dbContext.Database.Migrate();
                }
                catch (Exception)
                {
                    continue;
                }

                break;
            }

            dbContext.Dispose();

            var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
            {
                var rabbitmqHostName = configuration.BrokerHost;

                var rabbitMqConnectionString = $"rabbitmq://{rabbitmqHostName}";

                var RABBITMQ_HOST = Environment.GetEnvironmentVariable("RABBITMQ_HOST") ?? rabbitmqHostName;

                rabbitMqConnectionString = rabbitMqConnectionString.Replace(rabbitmqHostName, RABBITMQ_HOST);

                var host = sbc.Host(new Uri(rabbitMqConnectionString), h =>
                {
                    h.Username("guest");
                    h.Password("guest");
                });

                sbc.ReceiveEndpoint(host, ep =>
                {
                    ep.Consumer <StateRequestHandler>();
                });

                sbc.ReceiveEndpoint(host, ep =>
                {
                    ep.Consumer <TownsRequestHandler>();
                });
            });

            Console.Out.WriteLine("Starting service bus");

            bus.Start();

            Console.WriteLine("Press any key to exit");
            Done.Wait();

            bus.Stop();
        }