-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathExcelConfigurationReader.cs
More file actions
155 lines (150 loc) · 6.09 KB
/
ExcelConfigurationReader.cs
File metadata and controls
155 lines (150 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
08/19/2022 EPPlus Software AB Implementing handling of initialization errors in ExcelPackage class.
*************************************************************************************************/
#if (Core)
using Microsoft.Extensions.Configuration;
#else
using System.Configuration;
#endif
using OfficeOpenXml.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace OfficeOpenXml
{
internal static class ExcelConfigurationReader
{
/// <summary>
/// Reads an environment variable from the o/s. If an error occors it will rethrow the <see cref="Exception"/> unless SuppressInitializationExceptions of the <paramref name="config"/> is set to true.
/// </summary>
/// <param name="key">The key of the requested variable</param>
/// <param name="target">The <see cref="EnvironmentVariableTarget"/></param>
/// <param name="config">Configuration of the package</param>
/// <param name="initErrors">A list of logged <see cref="ExcelInitializationError"/> objects.</param>
/// <returns>The value of the environment variable</returns>
internal static string GetEnvironmentVariable(string key, EnvironmentVariableTarget target, ExcelPackageConfiguration config, List<ExcelInitializationError> initErrors)
{
var supressInitExceptions = config.SuppressInitializationExceptions;
try
{
return Environment.GetEnvironmentVariable(key, target);
}
catch (Exception ex)
{
if (supressInitExceptions)
{
var errorMessage = $"Could not read environment variable \"{key}\"";
var error = new ExcelInitializationError(errorMessage, ex);
initErrors.Add(error);
}
else
{
throw;
}
}
return default;
}
#if (Core)
internal static string GetJsonConfigValue(string key, ExcelPackageConfiguration config, List<ExcelInitializationError> initErrors)
{
var supressInitExceptions = config.SuppressInitializationExceptions;
// try to read from the IConfiguration that can be supplied either via constructor of ExcelPackage or ExcelPackage.Configure
var cfg = ExcelPackage.Configuration;
if(cfg == null && config.Configuration != null)
{
cfg = config.Configuration;
}
if(cfg != null)
{
try
{
var v = cfg[key];
return v;
}
catch(Exception ex)
{
var errorMessage = $"Could read value '{key}' from the supplied instance of IConfiguration.";
var error = new ExcelInitializationError(errorMessage, ex);
initErrors.Add(error);
return null;
}
}
// use file paths of the configuration
var basePath = config.JsonConfigBasePath;
var configFileName = config.JsonConfigFileName;
var configRoot = default(IConfigurationRoot);
try
{
var build = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile(configFileName, true, false);
configRoot = build.Build();
}
catch (Exception ex)
{
if (supressInitExceptions)
{
var errorMessage = $"Could not load configuration file \"{configFileName}\"";
var error = new ExcelInitializationError(errorMessage, ex);
initErrors.Add(error);
}
else
{
throw;
}
}
if (configRoot != null)
{
try
{
var v = configRoot[key];
return v;
}
catch (Exception ex)
{
if (supressInitExceptions)
{
var errorMessage = $"Could read key \"{key}\" from appsettings.json";
var error = new ExcelInitializationError(errorMessage, ex);
initErrors.Add(error);
return null;
}
throw;
}
}
return null;
}
#endif
#if (!Core)
internal static string GetValueFromAppSettings(string key, ExcelPackageConfiguration config, List<ExcelInitializationError> initErrors)
{
var supressInitExceptions = config.SuppressInitializationExceptions;
try
{
return ConfigurationManager.AppSettings[key];
}
catch(Exception ex)
{
if (supressInitExceptions)
{
var errorMessage = $"Could read key \"{key}\" from ConfigurationManager.AppSettings";
var error = new ExcelInitializationError(errorMessage, ex);
initErrors.Add(error);
return null;
}
throw;
}
}
#endif
}
}