Skip to content

Commit d38a9be

Browse files
committed
Support legacy IApplicationBuilder
1 parent fd284df commit d38a9be

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/NetLah.Extensions.HttpOverrides/HttpOverridesExtensions.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace NetLah.Extensions.HttpOverrides;
1515
public static class HttpOverridesExtensions
1616
{
1717
private static readonly Lazy<ILogger?> _loggerLazy = new(() => AppLogReference.GetAppLogLogger(typeof(HttpOverridesExtensions).Namespace));
18-
private static HealthCheckAppOptions _healthCheckAppOptions = default!;
18+
private static HealthCheckAppOptions? _healthCheckAppOptions;
1919
private static bool _isForwardedHeadersEnabled;
2020
private static bool _isHttpLoggingEnabled;
2121
private static LogLevel _httpOverridesLogLevel = LogLevel.Information;
@@ -154,11 +154,24 @@ ILogger EnsureLogger()
154154
return services;
155155
}
156156

157-
public static WebApplication UseHttpOverrides(this WebApplication app, ILogger? logger = null)
157+
public static WebApplication UseHttpOverrides(this WebApplication app, ILogger? logger = null) => InternalUseHttpOverrides(app, logger);
158+
159+
public static IApplicationBuilder UseHttpOverrides(this IApplicationBuilder app, ILogger? logger = null) => InternalUseHttpOverrides(app, logger);
160+
161+
private static TApplication InternalUseHttpOverrides<TApplication>(TApplication app, ILogger? logger) where TApplication : IApplicationBuilder
158162
{
159163
logger ??= _loggerLazy.Value;
160164
logger ??= NullLogger.Instance;
161-
var sp = app.Services;
165+
166+
if (_healthCheckAppOptions == null)
167+
{
168+
logger.LogCritical("Run builder.AddHttpOverrides() first");
169+
#pragma warning disable S112 // General exceptions should never be thrown
170+
throw new ApplicationException("Run builder.AddHttpOverrides() first");
171+
#pragma warning restore S112 // General exceptions should never be thrown
172+
}
173+
174+
var sp = app.ApplicationServices;
162175
var optionsForwardedHeadersOptions = sp.GetRequiredService<IOptions<ForwardedHeadersOptions>>();
163176
var fho = optionsForwardedHeadersOptions.Value;
164177

0 commit comments

Comments
 (0)