Ejemplo n.º 1
0
        public AppSettings()
        {
            try
            {
                var memoryCacheProvider = MemoryCacheProvider.GetInstance();
                var filePathKey         = $"{MethodBase.GetCurrentMethod()?.DeclaringType?.FullName}.FilePath";
                var filePath            = (object)memoryCacheProvider.Get(filePathKey);
                if (null == filePath)
                {
                    var appSettingsSetupFilePath         = Path.Combine(BaseDirectory !, SetupFileName !);
                    var appSettingsUserFilePath          = Path.Combine(UserProfileDirectory !, FileName !);
                    var appSettingsFilePath              = FilePath;
                    var appSettingsSetupConnectionString =
                        new AppSettings(appSettingsSetupFilePath !).GetConnectionString();
                    var appSettingsUserConnectionString =
                        new AppSettings(appSettingsUserFilePath !).GetConnectionString();
                    var appSettingsConnectionString =
                        new AppSettings(appSettingsFilePath !).GetConnectionString();

                    if (!string.IsNullOrWhiteSpace(appSettingsUserFilePath) && !File.Exists(appSettingsUserFilePath))
                    {
                        AppSettingsRepository?.CopyToUserDirectory(this);
                    }

                    try
                    {
                        AppSettingsRepository?.MergeAndSave(appSettingsSetupFilePath, appSettingsUserFilePath);
                    }
                    catch (Exception e)
                    {
                        _log4Net.Error($"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                       e);
                    }

                    try
                    {
                        if (File.Exists(appSettingsSetupFilePath))
                        {
                            File.Delete(appSettingsSetupFilePath);
                        }
                    }
                    catch (Exception e)
                    {
                        _log4Net.Error($"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                       e);
                    }

                    try
                    {
                        AppSettingsRepository?.MergeAndSave(appSettingsUserFilePath, appSettingsFilePath);
                    }
                    catch (Exception e)
                    {
                        _log4Net.Error($"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                       e);
                    }

                    try
                    {
                        AppSettingsRepository?.MergeAndSave(appSettingsFilePath, appSettingsUserFilePath);
                    }
                    catch (Exception e)
                    {
                        _log4Net.Error($"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                       e);
                    }

                    FilePath = File.Exists(appSettingsUserFilePath) ? appSettingsUserFilePath : FilePath;

                    if (null != AppSettingsRepository)
                    {
                        if (!string.IsNullOrWhiteSpace(appSettingsSetupConnectionString) &&
                            AppSettingsRepository.MssqlCheckConnectionString(appSettingsSetupConnectionString))
                        {
                            ConnectionString = appSettingsSetupConnectionString;
                            AppSettingsRepository?.SaveAsync(this);
                        }
                        else if (!string.IsNullOrWhiteSpace(appSettingsUserConnectionString) &&
                                 AppSettingsRepository.MssqlCheckConnectionString(appSettingsUserConnectionString))
                        {
                            ConnectionString = appSettingsUserConnectionString;
                            AppSettingsRepository?.SaveAsync(this);
                        }
                        else if (!string.IsNullOrWhiteSpace(appSettingsConnectionString) &&
                                 AppSettingsRepository.MssqlCheckConnectionString(appSettingsConnectionString))
                        {
                            ConnectionString = appSettingsConnectionString;
                            AppSettingsRepository?.SaveAsync(this);
                        }
                    }

                    memoryCacheProvider.Put(filePathKey, FilePath, TimeSpan.FromDays(1));
                }

                if (null != UserProfileDirectory && null != FileName)
                {
                    FilePath = (string)(filePath ?? Path.Combine(UserProfileDirectory !, FileName !));
                }
            }
            catch (Exception e)
            {
                _log4Net.Error($"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n", e);
            }
        }