Skip to content

Commit c829946

Browse files
committed
Add more client factory overloads
Fixes: dotnet/extensions#814 This change makes a few existing features more discoverable by adding first-class overloads on `AddHttpClient`. \n\nCommit migrated from dotnet/extensions@5fe6ff7
1 parent 7643d3d commit c829946

File tree

4 files changed

+313
-0
lines changed

4 files changed

+313
-0
lines changed

src/HttpClientFactory/Http/ref/Microsoft.Extensions.Http.netcoreapp.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ public static partial class HttpClientFactoryServiceCollectionExtensions
3737
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) where TClient : class where TImplementation : class, TClient { throw null; }
3838
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<System.IServiceProvider, System.Net.Http.HttpClient> configureClient) where TClient : class where TImplementation : class, TClient { throw null; }
3939
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<System.Net.Http.HttpClient> configureClient) where TClient : class where TImplementation : class, TClient { throw null; }
40+
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Func<System.Net.Http.HttpClient, System.IServiceProvider, TImplementation> factory) where TClient : class where TImplementation : class, TClient { throw null; }
41+
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Func<System.Net.Http.HttpClient, TImplementation> factory) where TClient : class where TImplementation : class, TClient { throw null; }
4042
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string name) where TClient : class where TImplementation : class, TClient { throw null; }
4143
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string name, System.Action<System.IServiceProvider, System.Net.Http.HttpClient> configureClient) where TClient : class where TImplementation : class, TClient { throw null; }
4244
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string name, System.Action<System.Net.Http.HttpClient> configureClient) where TClient : class where TImplementation : class, TClient { throw null; }
45+
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string name, System.Func<System.Net.Http.HttpClient, System.IServiceProvider, TImplementation> factory) where TClient : class where TImplementation : class, TClient { throw null; }
46+
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string name, System.Func<System.Net.Http.HttpClient, TImplementation> factory) where TClient : class where TImplementation : class, TClient { throw null; }
4347
}
4448
public partial interface IHttpClientBuilder
4549
{

src/HttpClientFactory/Http/ref/Microsoft.Extensions.Http.netstandard2.0.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ public static partial class HttpClientFactoryServiceCollectionExtensions
3737
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) where TClient : class where TImplementation : class, TClient { throw null; }
3838
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<System.IServiceProvider, System.Net.Http.HttpClient> configureClient) where TClient : class where TImplementation : class, TClient { throw null; }
3939
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<System.Net.Http.HttpClient> configureClient) where TClient : class where TImplementation : class, TClient { throw null; }
40+
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Func<System.Net.Http.HttpClient, System.IServiceProvider, TImplementation> factory) where TClient : class where TImplementation : class, TClient { throw null; }
41+
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Func<System.Net.Http.HttpClient, TImplementation> factory) where TClient : class where TImplementation : class, TClient { throw null; }
4042
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string name) where TClient : class where TImplementation : class, TClient { throw null; }
4143
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string name, System.Action<System.IServiceProvider, System.Net.Http.HttpClient> configureClient) where TClient : class where TImplementation : class, TClient { throw null; }
4244
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string name, System.Action<System.Net.Http.HttpClient> configureClient) where TClient : class where TImplementation : class, TClient { throw null; }
45+
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string name, System.Func<System.Net.Http.HttpClient, System.IServiceProvider, TImplementation> factory) where TClient : class where TImplementation : class, TClient { throw null; }
46+
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string name, System.Func<System.Net.Http.HttpClient, TImplementation> factory) where TClient : class where TImplementation : class, TClient { throw null; }
4347
}
4448
public partial interface IHttpClientBuilder
4549
{

src/HttpClientFactory/Http/src/DependencyInjection/HttpClientFactoryServiceCollectionExtensions.cs

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,5 +760,197 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS
760760
builder.AddTypedClientCore<TClient, TImplementation>(validateSingleType: false); // name was explicitly provided
761761
return builder;
762762
}
763+
764+
/// <summary>
765+
/// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures
766+
/// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>.
767+
/// </summary>
768+
/// <typeparam name="TClient">
769+
/// The type of the typed client. The type specified will be registered in the service collection as
770+
/// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients.
771+
/// </typeparam>
772+
/// <typeparam name="TImplementation">
773+
/// The implementation type of the typed client.
774+
/// </typeparam>
775+
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
776+
/// <param name="factory">A delegate that is used to create an instance of <typeparamref name="TClient"/>.</param>
777+
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
778+
/// <remarks>
779+
/// <para>
780+
/// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using
781+
/// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name.
782+
/// </para>
783+
/// <para>
784+
/// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" />
785+
/// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing
786+
/// <typeparamref name="TClient"/> as the service type.
787+
/// </para>
788+
/// </remarks>
789+
public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IServiceCollection services, Func<HttpClient, TImplementation> factory)
790+
where TClient : class
791+
where TImplementation : class, TClient
792+
{
793+
if (services == null)
794+
{
795+
throw new ArgumentNullException(nameof(services));
796+
}
797+
798+
if (factory == null)
799+
{
800+
throw new ArgumentNullException(nameof(factory));
801+
}
802+
803+
var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);
804+
return AddHttpClient<TClient, TImplementation>(services, name, factory);
805+
}
806+
807+
/// <summary>
808+
/// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures
809+
/// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>.
810+
/// </summary>
811+
/// <typeparam name="TClient">
812+
/// The type of the typed client. The type specified will be registered in the service collection as
813+
/// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients.
814+
/// </typeparam>
815+
/// <typeparam name="TImplementation">
816+
/// The implementation type of the typed client.
817+
/// </typeparam>
818+
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
819+
/// <param name="name">The logical name of the <see cref="HttpClient"/> to configure.</param>
820+
/// <param name="factory">A delegate that is used to create an instance of <typeparamref name="TClient"/>.</param>
821+
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
822+
/// <remarks>
823+
/// <para>
824+
/// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using
825+
/// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name.
826+
/// </para>
827+
/// <para>
828+
/// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" />
829+
/// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing
830+
/// <typeparamref name="TClient"/> as the service type.
831+
/// </para>
832+
/// <typeparamref name="TImplementation">
833+
/// </typeparamref>
834+
/// </remarks>
835+
public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IServiceCollection services, string name, Func<HttpClient, TImplementation> factory)
836+
where TClient : class
837+
where TImplementation : class, TClient
838+
{
839+
if (services == null)
840+
{
841+
throw new ArgumentNullException(nameof(services));
842+
}
843+
844+
if (name == null)
845+
{
846+
throw new ArgumentNullException(nameof(name));
847+
}
848+
849+
if (factory == null)
850+
{
851+
throw new ArgumentNullException(nameof(factory));
852+
}
853+
854+
AddHttpClient(services);
855+
856+
var builder = new DefaultHttpClientBuilder(services, name);
857+
builder.AddTypedClient<TClient>(factory);
858+
return builder;
859+
}
860+
861+
/// <summary>
862+
/// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures
863+
/// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>.
864+
/// </summary>
865+
/// <typeparam name="TClient">
866+
/// The type of the typed client. The type specified will be registered in the service collection as
867+
/// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients.
868+
/// </typeparam>
869+
/// <typeparam name="TImplementation">
870+
/// The implementation type of the typed client.
871+
/// </typeparam>
872+
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
873+
/// <param name="factory">A delegate that is used to create an instance of <typeparamref name="TClient"/>.</param>
874+
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
875+
/// <remarks>
876+
/// <para>
877+
/// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using
878+
/// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name.
879+
/// </para>
880+
/// <para>
881+
/// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" />
882+
/// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing
883+
/// <typeparamref name="TClient"/> as the service type.
884+
/// </para>
885+
/// </remarks>
886+
public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IServiceCollection services, Func<HttpClient, IServiceProvider, TImplementation> factory)
887+
where TClient : class
888+
where TImplementation : class, TClient
889+
{
890+
if (services == null)
891+
{
892+
throw new ArgumentNullException(nameof(services));
893+
}
894+
895+
if (factory == null)
896+
{
897+
throw new ArgumentNullException(nameof(factory));
898+
}
899+
900+
var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);
901+
return AddHttpClient<TClient, TImplementation>(services, name, factory);
902+
}
903+
904+
/// <summary>
905+
/// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures
906+
/// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>.
907+
/// </summary>
908+
/// <typeparam name="TClient">
909+
/// The type of the typed client. The type specified will be registered in the service collection as
910+
/// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients.
911+
/// </typeparam>
912+
/// <typeparam name="TImplementation">
913+
/// The implementation type of the typed client.
914+
/// </typeparam>
915+
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
916+
/// <param name="name">The logical name of the <see cref="HttpClient"/> to configure.</param>
917+
/// <param name="factory">A delegate that is used to create an instance of <typeparamref name="TClient"/>.</param>
918+
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
919+
/// <remarks>
920+
/// <para>
921+
/// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using
922+
/// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name.
923+
/// </para>
924+
/// <para>
925+
/// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" />
926+
/// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing
927+
/// <typeparamref name="TClient"/> as the service type.
928+
/// </para>
929+
/// </remarks>
930+
public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IServiceCollection services, string name, Func<HttpClient, IServiceProvider, TImplementation> factory)
931+
where TClient : class
932+
where TImplementation : class, TClient
933+
{
934+
if (services == null)
935+
{
936+
throw new ArgumentNullException(nameof(services));
937+
}
938+
939+
if (name == null)
940+
{
941+
throw new ArgumentNullException(nameof(name));
942+
}
943+
944+
if (factory == null)
945+
{
946+
throw new ArgumentNullException(nameof(factory));
947+
}
948+
949+
AddHttpClient(services);
950+
951+
var builder = new DefaultHttpClientBuilder(services, name);
952+
builder.AddTypedClient<TClient>(factory);
953+
return builder;
954+
}
763955
}
764956
}

0 commit comments

Comments
 (0)