AspNetCore.Proxy icon indicating copy to clipboard operation
AspNetCore.Proxy copied to clipboard

NullReferenceException when GetService<IHttpClientFactory>() fails

Open plutora-steve-g opened this issue 3 years ago • 1 comments

In ExecuteHttpProxyOperationAsync the following code throws if IHttpClientFactory is not registered. Wasn't obvious to me what the cause was, ended up downloading the code and stepping through

                var httpClient = context.RequestServices
                    .GetService<IHttpClientFactory>()
                    .CreateClient(options?.HttpClientName ?? Helpers.HttpProxyClientName);

Perhaps something along these lines:

                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);

plutora-steve-g avatar Jun 21 '22 03:06 plutora-steve-g

Good idea: if you want to make the PR, then you can get credit. 😄

Otherwise, I am happy to do it.

twitchax avatar Jun 22 '22 04:06 twitchax