Exemple #1
0
        public async Task <IActionResult> Post()
        {
            using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                var jsonContent = await reader.ReadToEndAsync();

                // Check the event type.
                // Return the validation code if it's
                // a subscription validation request.
                if (EventTypeSubcriptionValidation)
                {
                    return(await HandleValidation(jsonContent));
                }
                else if (EventTypeNotification)
                {
                    // Check to see if this is passed in using
                    // the CloudEvents schema
                    if (IsCloudEvent(jsonContent))
                    {
                        _refresher.SetDirty(TimeSpan.Zero);
                        return(await HandleCloudEvent(jsonContent));
                    }

                    _refresher.SetDirty(TimeSpan.Zero);
                    return(await HandleGridEvents(jsonContent));
                }

                return(BadRequest());
            }
        }
Exemple #2
0
        public void RefreshTests_SetDirtyForcesNextRefresh()
        {
            IConfigurationRefresher refresher = null;
            var mockClient = GetMockConfigurationClient();

            var config = new ConfigurationBuilder()
                         .AddAzureAppConfiguration(options =>
            {
                options.Client = mockClient.Object;
                options.Select("TestKey*");
                options.ConfigureRefresh(refreshOptions =>
                {
                    refreshOptions.Register("TestKey1", "label")
                    .SetCacheExpiration(TimeSpan.FromDays(1));
                });

                refresher = options.GetRefresher();
            })
                         .Build();

            Assert.Equal("TestValue1", config["TestKey1"]);
            FirstKeyValue.Value = "newValue";

            refresher.RefreshAsync().Wait();
            Assert.Equal("TestValue1", config["TestKey1"]);

            refresher.SetDirty(TimeSpan.FromSeconds(1));

            // Wait for the cache to expire based on the randomized delay in SetDirty()
            Thread.Sleep(1200);

            refresher.RefreshAsync().Wait();
            Assert.Equal("newValue", config["TestKey1"]);
        }
Exemple #3
0
        public async Task OnGet()
        {
            _configurationRefresher.SetDirty(TimeSpan.FromSeconds(1));
            await Task.Delay(1000);

            bool success = await _configurationRefresher.TryRefreshAsync();
        }
Exemple #4
0
        public async Task Run([EventGridTrigger] EventGridEvent eventGridEvent)
        {
            var sentinel = _configuration["appsection:sentinel"];

            // Invalidate cached key-values before calling TryRefreshAsync
            _configurationRefresher.SetDirty(TimeSpan.FromSeconds(0));
            await _configurationRefresher.TryRefreshAsync();

            while (sentinel == _configuration["appsection:sentinel"])
            {
                Console.WriteLine(
                    $"Sentinel value is not updated yet: {_configuration["appsection:sentinel"]}");
                await Task.Delay(TimeSpan.FromSeconds(1));

                await _configurationRefresher.TryRefreshAsync();
            }
            var message = new Dictionary <string, string>
            {
                { "appsection:key1", _configuration["appsection:key1"] },
                { "appsection:key2", _configuration.GetValue <string>("appsection:key2") },
                { "appsection:sentinel", _configuration["appsection:sentinel"] }
            };

            Console.WriteLine(JsonConvert.SerializeObject(message));
        }
Exemple #5
0
        private Task ProcessEventHandler(ProcessEventArgs eventArgs)
        {
            _logger.LogInformation("EventHub update received. Triggering cache invalidation.");

            //
            // Set the cached value for key-values registered for refresh as dirty.
            _configurationRefresher?.SetDirty();

            return(Task.CompletedTask);
        }
Exemple #6
0
        public void Run(
            [EventGridTrigger] EventGridEvent eventGridEvent,
            ILogger log)
        {
            log.LogInformation(eventGridEvent.Data.ToString());

            // A random delay is added before the cached value is marked as dirty to reduce potential
            // throttling in case multiple instances refresh at the same time.
            _configurationRefresher.SetDirty();
        }
        private static void RegisterRefreshEventHandler(IConfiguration configuration)
        {
            var connectionString       = configuration.GetConnectionString("ServiceBus");
            var topicName              = configuration.GetValue <string>("AppConfigurationEventHandling:TopicName");
            var serviceBusSubscription = configuration.GetValue <string>("AppConfigurationEventHandling:ServiceBusSubscription");

            var serviceBusClient = new SubscriptionClient(connectionString, topicName, serviceBusSubscription);

            serviceBusClient.RegisterMessageHandler((message, cancellationToken) => {
                var messageText = Encoding.UTF8.GetString(message.Body);
                var messageData = JsonDocument.Parse(messageText).RootElement.GetProperty("data");
                var key         = messageData.GetProperty("key").GetString();
                Console.WriteLine($"Event received for Key: {key}");
                _refresher.SetDirty();
                return(Task.CompletedTask);
            }, (exceptionArgs) => {
                Console.WriteLine($"{exceptionArgs.Exception}");
                return(Task.CompletedTask);
            });
        }