From 090b280df2ce3781e8a1681a79d6c5a5239faecb Mon Sep 17 00:00:00 2001 From: senthil Date: Tue, 24 Dec 2024 08:18:41 +1300 Subject: [PATCH] Bring you own httpclient (#3) --- IpApi.Net.Sample.Console/Program.cs | 3 ++- IpApi.Net.sln | 5 ++--- IpApi/DependencyInjection.cs | 20 +++++++++++++++++++- IpApi/IpApiClient.cs | 2 +- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/IpApi.Net.Sample.Console/Program.cs b/IpApi.Net.Sample.Console/Program.cs index 261b93e..5a84dc9 100644 --- a/IpApi.Net.Sample.Console/Program.cs +++ b/IpApi.Net.Sample.Console/Program.cs @@ -10,7 +10,8 @@ static async Task Main(string[] args) var host = Host.CreateDefaultBuilder(args) .ConfigureServices(services => { - services.AddIpApiClient(); + services.AddHttpClient(); + services.AddIpApiClient(useExistingHttpClient:true); services.AddScoped(); }) diff --git a/IpApi.Net.sln b/IpApi.Net.sln index 7ec205d..7599a42 100644 --- a/IpApi.Net.sln +++ b/IpApi.Net.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.12.35514.174 d17.12 +VisualStudioVersion = 17.12.35514.174 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IpApi", "IpApi\IpApi.csproj", "{2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}" EndProject @@ -13,8 +13,7 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}.Debug|Any CPU.ActiveCfg = Release|Any CPU - {2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}.Debug|Any CPU.Build.0 = Release|Any CPU + {2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}.Release|Any CPU.ActiveCfg = Release|Any CPU {2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}.Release|Any CPU.Build.0 = Release|Any CPU {5EB20BE2-F56E-4ABF-9F2F-958FB7AFA579}.Debug|Any CPU.ActiveCfg = Debug|Any CPU diff --git a/IpApi/DependencyInjection.cs b/IpApi/DependencyInjection.cs index cd3fae2..44c5120 100644 --- a/IpApi/DependencyInjection.cs +++ b/IpApi/DependencyInjection.cs @@ -4,8 +4,26 @@ namespace IpApi; public static class DependencyInjection { - public static IServiceCollection AddIpApiClient(this IServiceCollection serviceCollection) + /// + /// Adds the and IIApiClient to the . + /// Set true to if you are bringing you own httpclient. + /// + /// The . + /// The . + /// Set true if httpclient is already registered + public static IServiceCollection AddIpApiClient(this IServiceCollection serviceCollection, bool useExistingHttpClient = false) { + if (useExistingHttpClient) + { + serviceCollection.AddScoped(sp => + { + var httpClientFactory = sp.GetRequiredService(); + return new IpApiClient(httpClientFactory); + }); + + return serviceCollection; + } + serviceCollection.AddHttpClient(); serviceCollection.AddScoped(); diff --git a/IpApi/IpApiClient.cs b/IpApi/IpApiClient.cs index 6a85244..4bfdb23 100644 --- a/IpApi/IpApiClient.cs +++ b/IpApi/IpApiClient.cs @@ -55,7 +55,7 @@ private static async Task HandleErrorAsync(CancellationToken cancellationToken, using var httpClient = _httpClientFactory.CreateClient(); httpClient.BaseAddress = new Uri(BaseUrl); - var allIps = string.Join(" ", ips); + var allIps = string.Join(",", ips); var response = await httpClient.GetAsync(allIps, cancellationToken);