You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServiceCollectionExtensions.cs 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Microsoft.Extensions.DependencyInjection;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Net.Http;
  8. using System.Net;
  9. using Discord.Net.Rest;
  10. using Discord.Rest.Net;
  11. namespace Discord.Rest.Extensions
  12. {
  13. /// <summary>
  14. /// Here you will find all the extensions to be able to add a Discord Client to an IServiceCollection
  15. /// </summary>
  16. public static class ServiceCollectionExtensions
  17. {
  18. public static IServiceCollection AddScopedDiscordRestClient(this IServiceCollection services, bool useProxy = false)
  19. {
  20. services.AddHttpClient("HttpFactoryRestClientProvider")
  21. .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
  22. {
  23. AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
  24. UseCookies = false,
  25. UseProxy = useProxy,
  26. });
  27. services.AddTransient<HttpFactoryRestClientProvider>(provider => new HttpFactoryRestClientProvider(provider.GetRequiredService<IHttpClientFactory>()));
  28. services.AddScoped<DiscordRestClient>(provider =>
  29. {
  30. var config = new DiscordRestConfig
  31. {
  32. RestClientProvider = provider.GetRequiredService<HttpFactoryRestClientProvider>().Instance
  33. };
  34. return new DiscordRestClient(config);
  35. });
  36. return services;
  37. }
  38. 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.
  39. {
  40. services.AddHttpClient("HttpFactoryRestClientProvider")
  41. .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
  42. {
  43. AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
  44. UseCookies = false,
  45. UseProxy = useProxy,
  46. });
  47. services.AddTransient<HttpFactoryRestClientProvider>(provider => new HttpFactoryRestClientProvider(provider.GetRequiredService<IHttpClientFactory>()));
  48. services.AddTransient<DiscordRestClient>(provider =>
  49. {
  50. var config = new DiscordRestConfig
  51. {
  52. RestClientProvider = provider.GetRequiredService<HttpFactoryRestClientProvider>().Instance
  53. };
  54. return new DiscordRestClient(config);
  55. });
  56. return services;
  57. }
  58. }
  59. }