private void Load(bool reload)
        {
            var file = Source.FileProvider?.GetFileInfo(Source.Path);

            if (file == null || !file.Exists)
            {
                if (Source.Optional || reload) // Always optional on reload
                {
                    Data = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                }
                else
                {
                    var error = new StringBuilder($"The configuration file '{Source.Path}' was not found and is not optional.");
                    if (!string.IsNullOrEmpty(file?.PhysicalPath))
                    {
                        error.Append($" The physical path is '{file.PhysicalPath}'.");
                    }
                    throw new FileNotFoundException(error.ToString());
                }
            }
            else
            {
                // Always create new Data on reload to drop old keys
                if (reload)
                {
                    Data = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                }
                using (var stream = file.CreateReadStream())
                {
                    try
                    {
                        Load(stream);
                    }
                    catch (Exception e)
                    {
                        bool ignoreException = false;
                        if (Source.OnLoadException != null)
                        {
                            var exceptionContext = new FileLoadExceptionContext
                            {
                                Provider  = this,
                                Exception = e
                            };
                            Source.OnLoadException.Invoke(exceptionContext);
                            ignoreException = exceptionContext.Ignore;
                        }
                        if (!ignoreException)
                        {
                            throw e;
                        }
                    }
                }
            }
            // REVIEW: Should we raise this in the base as well / instead?
            OnReload();
        }
Example #2
0
        private void HandleException(Exception e)
        {
            bool ignoreException = false;

            if (Source.OnLoadException != null)
            {
                var exceptionContext = new FileLoadExceptionContext
                {
                    Provider  = this,
                    Exception = e
                };
                Source.OnLoadException.Invoke(exceptionContext);
                ignoreException = exceptionContext.Ignore;
            }
            if (!ignoreException)
            {
                throw e;
            }
        }
        private void HandleException(ExceptionDispatchInfo info)
        {
            bool ignoreException = false;

            if (Source.OnLoadException != null)
            {
                var exceptionContext = new FileLoadExceptionContext
                {
                    Provider  = this,
                    Exception = info.SourceException
                };
                Source.OnLoadException.Invoke(exceptionContext);
                ignoreException = exceptionContext.Ignore;
            }
            if (!ignoreException)
            {
                info.Throw();
            }
        }