diff --git a/samples/05_simple_blazor_discord_login/Extensions/ServiceCollectionExtensions.cs b/samples/05_simple_blazor_discord_login/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 000000000..62e0c1610 --- /dev/null +++ b/samples/05_simple_blazor_discord_login/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,53 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net.Http; +using System.Net; +using Discord.Net.Rest; +using Discord.Rest; + +namespace _05_simple_blazor_discord_login.Extensions +{ + /// + /// Here you will find all the extensions to be able to add a Discord Client to an IServiceCollection + /// + public static class ServiceCollectionExtensions + { + /// + /// Adds the DiscordRestClient as a Scoped 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 AddDiscordRestClient(this IServiceCollection services, string clientId = null, string clientSecret = null, bool useProxy = false) + { + services.AddHttpClient("HttpClientFactoryRestClientProvider") + .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler + { + AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, + UseCookies = false, + UseProxy = useProxy, + }); + + + + //services.AddScoped(provider => new HttpClientFactoryRestClientProvider(provider.GetRequiredService())); + services.AddSingleton(provider => + { + var config = new DiscordRestConfig + { + RestClientProvider = HttpClientFactoryRestClientProvider.Create(provider.GetRequiredService().CreateClient("HttpClientFactoryRestClientProvider"), useProxy), + ClientId = clientId, + ClientSecret = clientSecret + + }; + return new DiscordRestClient(config); + }); + + return services; + } + } +}