Browse Source

Added two Extension methods "AddScopedDiscordRestClient" and "AddTransientDiscordRestClient" to allow for easier addition of scoped, and transient DiscordRestClients, it's left up to the user which one is more convenient to use for his/her application, and at the end of the day it doesn't risk running out of sockets since it uses IHttpClientFactory under the hood.

This means that it moves the useProxy boolean parameter here, not sure how much that affects the rest of the library. def an addition to consider. And something that more accustomed users/contributors will be able to answer.

The main advantage of having such a method in the future could be adding resiliency. Major resiliency by for example using Polly in a simple way see: https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/implement-http-call-retries-exponential-backoff-polly for example.

Of course this means that the user would have to use DI (which is anyways a good practice), but I left the other default ones so that it wouldn't bring any breaking changes.
pull/1806/head
emorell96 4 years ago
parent
commit
b745a3cd6d
1 changed files with 69 additions and 0 deletions
  1. +69
    -0
      src/Discord.Net.Rest/Extensions/ServiceCollectionExtensions.cs

+ 69
- 0
src/Discord.Net.Rest/Extensions/ServiceCollectionExtensions.cs View File

@@ -0,0 +1,69 @@
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.Net;

namespace Discord.Rest.Extensions
{
/// <summary>
/// Here you will find all the extensions to be able to add a Discord Client to an IServiceCollection
/// </summary>
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddScopedDiscordRestClient(this IServiceCollection services, bool useProxy = false)
{
services.AddHttpClient("HttpFactoryRestClientProvider")
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
UseCookies = false,
UseProxy = useProxy,
});



services.AddTransient<HttpFactoryRestClientProvider>(provider => new HttpFactoryRestClientProvider(provider.GetRequiredService<IHttpClientFactory>()));
services.AddScoped<DiscordRestClient>(provider =>
{
var config = new DiscordRestConfig
{
RestClientProvider = provider.GetRequiredService<HttpFactoryRestClientProvider>().Instance
};
return new DiscordRestClient(config);
});

return services;
}

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("HttpFactoryRestClientProvider")
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
UseCookies = false,
UseProxy = useProxy,
});



services.AddTransient<HttpFactoryRestClientProvider>(provider => new HttpFactoryRestClientProvider(provider.GetRequiredService<IHttpClientFactory>()));
services.AddTransient<DiscordRestClient>(provider =>
{
var config = new DiscordRestConfig
{
RestClientProvider = provider.GetRequiredService<HttpFactoryRestClientProvider>().Instance
};
return new DiscordRestClient(config);
});

return services;
}
}
}

Loading…
Cancel
Save