コード例 #1
0
        private async Task WatchDatabase(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    await Task.Delay(_configurationSource.PollingInterval, cancellationToken);

                    IDictionary <string, string> actualData = await GetDataAsync();

                    byte[] computedHash = ComputeHash(actualData);
                    if (!computedHash.SequenceEqual(_lastComputedHash))
                    {
                        Data = actualData;
                        OnReload();
                    }
                    _lastComputedHash = computedHash;
                }
                catch (Exception ex)
                {
                    var exceptionContext = new ConfigurationEFCoreLoadExceptionContext <TDbContext>(_configurationSource, ex);
                    _configurationSource.OnLoadException?.Invoke(exceptionContext);
                    if (!exceptionContext.Ignore)
                    {
                        throw;
                    }
                }
            }
        }
コード例 #2
0
        public override void Load()
        {
            if (_watchDbTask != null)
            {
                return;
            }

            try
            {
                Data = GetData();
                _lastComputedHash = ComputeHash(Data);
            }
            catch (Exception ex)
            {
                var exceptionContext = new ConfigurationEFCoreLoadExceptionContext <TDbContext>(_configurationSource, ex);
                _configurationSource.OnLoadException?.Invoke(exceptionContext);
                if (!exceptionContext.Ignore)
                {
                    throw;
                }
            }

            var cancellationToken = _cancellationTokenSource.Token;

            if (_configurationSource.ReloadOnChange)
            {
                _watchDbTask = Task.Run(() => WatchDatabase(cancellationToken), cancellationToken);
            }
        }