From 07bc94754813b919a5430e6f18128a7e9217b4bc Mon Sep 17 00:00:00 2001 From: emorell96 Date: Mon, 22 Mar 2021 13:11:15 -0700 Subject: [PATCH] modified the ServicesCollectionExtentions to only add Singleton DiscordRestClient so that the DiscordRestClient is a singleton and we do not lose the rate limiter but the HttpClient is still regenareted every time the provider is called through the IHttpClientFactory instead of using new HttpClient(new HttpClientHandler). --- .../Extensions/ServiceCollectionExtensions.cs | 34 ++----------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/src/Discord.Net.Rest/Extensions/ServiceCollectionExtensions.cs b/src/Discord.Net.Rest/Extensions/ServiceCollectionExtensions.cs index 57ce68b59..7b4123145 100644 --- a/src/Discord.Net.Rest/Extensions/ServiceCollectionExtensions.cs +++ b/src/Discord.Net.Rest/Extensions/ServiceCollectionExtensions.cs @@ -22,7 +22,7 @@ namespace Discord.Rest.Extensions /// This is the IServiceCollection where all the services are located. /// Set this to true to use proxies, default is false. /// - public static IServiceCollection AddScopedDiscordRestClient(this IServiceCollection services, bool useProxy = false) + public static IServiceCollection AddDiscordRestClient(this IServiceCollection services, bool useProxy = false) { services.AddHttpClient("HttpClientFactoryRestClientProvider") .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler @@ -35,37 +35,7 @@ namespace Discord.Rest.Extensions //services.AddScoped(provider => new HttpClientFactoryRestClientProvider(provider.GetRequiredService())); - services.AddScoped(provider => - { - var config = new DiscordRestConfig - { - RestClientProvider = url => new HttpClientFactoryRestClient(url, provider.GetRequiredService().CreateClient("HttpClientFactoryRestClientProvider"), useProxy) - }; - return new DiscordRestClient(config); - }); - - return services; - } - /// - /// Adds the DiscordRestClient as a Transient Service to be able to use through DI. - /// - /// This is the IServiceCollection where all the services are located. - /// Set this to true to use proxies, default is false. - /// - public static IServiceCollection AddTransientDiscordRestClient(this IServiceCollection services, bool useProxy = false) //where should we put this useProxy options, I haven't fully understood where the original code takes this from. - { - services.AddHttpClient("HttpClientFactoryRestClientProvider") - .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler - { - AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, - UseCookies = false, - UseProxy = useProxy, - }); - - - - //services.AddTransient(provider => new HttpClientFactoryRestClientProvider(provider.GetRequiredService())); - services.AddTransient(provider => + services.AddSingleton(provider => { var config = new DiscordRestConfig {