예제 #1
0
        private static async Task ExampleUsingRebusAsync()
        {
            _activator = new BuiltinHandlerActivator();

            string rabbitMqConnectionString = Configuration.GetConnectionString("RabbitMq");

            ConnectToRebus(rabbitMqConnectionString, _activator);

            // Subscribe to messages we want to handle
            _bus.Subscribe <UserLoginResponse>().Wait();
            _bus.Subscribe <ServiceConfigurationResponse>().Wait();

            while (true)
            {
                Thread.Sleep(4000);

                Guid requestId = Guid.NewGuid();
                Console.WriteLine($"Sending UserLoginRequest request. Request ID: {requestId}");
                UserLoginResponse userLoginResponse = await _bus.SendRequest <UserLoginResponse>(new UserLoginRequest(requestId, InputQueueName, "*****@*****.**", "dsfifigfdg"), RebusConfiguration.Headers, TimeSpan.FromSeconds(10));

                Console.WriteLine($"UserLoginResponse received. Request ID: {userLoginResponse.RequestMessageId}, Email: {userLoginResponse.Email}, ResultCode: {userLoginResponse.ResultCode}");

                Thread.Sleep(4000);

                requestId = Guid.NewGuid();
                Console.WriteLine($"Sending ServiceConfigurationRequest request. Request ID: {requestId}");
                ServiceConfigurationBundle[] serviceConfigurationBundles  = new ServiceConfigurationBundle[] { new ServiceConfigurationBundle("MyService", "Bundle1") };
                ServiceConfigurationResponse serviceConfigurationResponse = await _bus.SendRequest <ServiceConfigurationResponse>(new ServiceConfigurationRequest(requestId, InputQueueName, serviceConfigurationBundles), RebusConfiguration.Headers, TimeSpan.FromSeconds(10));

                Console.WriteLine($"ServiceConfigurationResponse received. Request ID: {serviceConfigurationResponse.RequestMessageId}");
            }
        }
        private void ProcessResponseAndUpdateConfig(ServiceConfigurationResponse serviceConfigurationResponse)
        {
            var ints     = new Dictionary <string, Setting <int> >();
            var booleans = new Dictionary <string, Setting <bool> >();
            var strings  = new Dictionary <string, Setting <string> >();

            foreach (var item in serviceConfigurationResponse.ConfigurationItems)
            {
                switch (item.Type)
                {
                case ConfigurationItemType.Boolean:
                    booleans.Add(item.Name, new Setting <bool>(bool.Parse(item.Value)));
                    break;

                case ConfigurationItemType.String:
                    strings.Add(item.Name, new Setting <string>(item.Value));
                    break;

                case ConfigurationItemType.Integer:
                    ints.Add(item.Name, new Setting <int>(int.Parse(item.Value)));
                    break;

                default:
                    _logger.LogWarning($"unknown type {item.Type}");
                    break;
                }
            }

            _settingsCacheManager.ReloadSettingsCache(new SettingsCache <int>(ints), new SettingsCache <bool>(booleans), new SettingsCache <string>(strings));
        }
        public async Task <ActionResult <ServiceConfigurationResponse> > GetUpdatedStatus(string serviceName, CancellationToken token)
        {
            _logger.LogInformation($"Received a configuration request for {serviceName}");
            string json;

            try {
                json = await _distributedCache.GetStringAsync(serviceName.ToLowerInvariant(), token).ConfigureAwait(false);

                if (json is null)
                {
                    _logger.LogWarning($"No settings found for {serviceName}");
                    return(NotFound());
                }
            }
            catch (Exception e) {
                _logger.LogError(e, $"Failed to read config section for {serviceName}");
                return(NotFound());
            }

            try {
                var page     = JsonConvert.DeserializeObject <ServiceConfigurationPage>(json);
                var response = new ServiceConfigurationResponse {
                    ConfigurationItems = page.Entries
                                         .Select(c => new ServiceConfigurationItem {
                        Type  = c.Type,
                        Value = c.Value,
                        Name  = c.Name
                    }).ToList()
                };
                return(response);
            }
            catch (Exception e) {
                _logger.LogError(e, $"Failed to serialize settings key for {serviceName}.");
                return(NotFound());
            }
        }
예제 #4
0
 private static async Task HandleServiceConfigurationResponse(ServiceConfigurationResponse msg)
 {
     Console.WriteLine($"ServiceConfigurationResponse received. Request ID: {msg.RequestMessageId}");
     await Task.CompletedTask;
 }