Skip to content

Commit

Permalink
Merge pull request #114 from amazur31/throw-error-for-null-factory
Browse files Browse the repository at this point in the history
Fix for NullReferenceException when GetService<IHttpClientFactory>() fails
  • Loading branch information
twitchax authored Aug 30, 2024
2 parents 3db53a6 + e9393fd commit bdc100b
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Core/Extensions/Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ internal static async Task ExecuteHttpProxyOperationAsync(this HttpContext conte

try
{
var httpClient = context.RequestServices
.GetService<IHttpClientFactory>()
.CreateClient(options?.HttpClientName ?? Helpers.HttpProxyClientName);
var clientFactory = context.RequestServices.GetService<IHttpClientFactory>()
?? throw new InvalidOperationException("IHttpClientFactory not registered. You need to add builder.Services.AddHttpClient(); during startup");

var httpClient = clientFactory.CreateClient(options?.HttpClientName ?? Helpers.HttpProxyClientName);

// If `true`, this proxy call has been intercepted.
if(options?.Intercept != null && await options.Intercept(context).ConfigureAwait(false))
Expand Down

0 comments on commit bdc100b

Please sign in to comment.