/// <summary> /// This is a custom method to register the HttpClient and typed factory. Needed because we need to access the config name when creating the typed client /// </summary> private static IHttpClientBuilder AddGrpcHttpClient <TClient>(this IServiceCollection services, string name, Action <IServiceProvider, HttpClient> configureClient) where TClient : class { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddHttpClient(); var builder = new DefaultHttpClientBuilder(services, name); builder.ConfigureHttpClient(configureClient); builder.Services.AddTransient <TClient>(s => { var httpClientFactory = s.GetRequiredService <IHttpClientFactory>(); var httpClient = httpClientFactory.CreateClient(builder.Name); var typedClientFactory = s.GetRequiredService <INamedTypedHttpClientFactory <TClient> >(); return(typedClientFactory.CreateClient(httpClient, builder.Name)); }); return(builder); }
/// <summary> /// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures /// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>. /// </summary> /// <typeparam name="TClient"> /// The type of the typed client. The type specified will be registered in the service collection as /// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients. /// </typeparam> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="name">The logical name of the <see cref="HttpClient"/> to configure.</param> /// <param name="configureClient">A delegate that is used to configure an <see cref="HttpClient"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> /// <remarks> /// <para> /// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using /// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name. /// </para> /// <para> /// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" /> /// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing /// <typeparamref name="TClient"/> as the service type. /// </para> /// <para> /// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client. /// </para> /// </remarks> public static IHttpClientBuilder AddHttpClient <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>( this IServiceCollection services, string name, Action <IServiceProvider, HttpClient> configureClient) where TClient : class { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } if (configureClient == null) { throw new ArgumentNullException(nameof(configureClient)); } AddHttpClient(services); var builder = new DefaultHttpClientBuilder(services, name); builder.ConfigureHttpClient(configureClient); builder.AddTypedClientCore <TClient>(validateSingleType: false); // name was explictly provided return(builder); }
/// <summary> /// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures /// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>. /// </summary> /// <typeparam name="TClient"> /// The type of the typed client. They type specified will be registered in the service collection as /// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients. /// </typeparam> /// <typeparam name="TImplementation"> /// The implementation type of the typed client. They type specified will be instantiated by the /// <see cref="ITypedHttpClientFactory{TImplementation}"/> /// </typeparam> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="name">The logical name of the <see cref="HttpClient"/> to configure.</param> /// <param name="configureClient">A delegate that is used to configure an <see cref="HttpClient"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> /// <remarks> /// <para> /// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using /// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name. /// </para> /// <para> /// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" /> /// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing /// <typeparamref name="TClient"/> as the service type. /// </para> /// <para> /// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client. /// </para> /// </remarks> public static IHttpClientBuilder AddHttpClient <TClient, TImplementation>(this IServiceCollection services, string name, Action <IServiceProvider, HttpClient> configureClient) where TClient : class where TImplementation : class, TClient { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } if (configureClient == null) { throw new ArgumentNullException(nameof(configureClient)); } AddHttpClient(services); var builder = new DefaultHttpClientBuilder(services, name); builder.ConfigureHttpClient(configureClient); builder.AddTypedClient <TClient, TImplementation>(); return(builder); }
/// <summary> /// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures /// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>. /// </summary> /// <typeparam name="TClient"> /// The type of the typed client. They type specified will be registered in the service collection as /// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients. /// </typeparam> /// <typeparam name="TImplementation"> /// The implementation type of the typed client. They type specified will be instantiated by the /// <see cref="ITypedHttpClientFactory{TImplementation}"/> /// </typeparam> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="name">The logical name of the <see cref="HttpClient"/> to configure.</param> /// <param name="configureClient">A delegate that is used to configure an <see cref="HttpClient"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> /// <remarks> /// <para> /// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using /// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name. /// </para> /// <para> /// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" /> /// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing /// <typeparamref name="TClient"/> as the service type. /// </para> /// <para> /// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client. /// </para> /// </remarks> public static IHttpClientBuilder AddHttpClient <TClient, TImplementation>(this IServiceCollection services, string name, Action <HttpClient> configureClient) where TClient : class where TImplementation : class, TClient { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } if (configureClient == null) { throw new ArgumentNullException(nameof(configureClient)); } AddHttpClient(services); var builder = new DefaultHttpClientBuilder(services, name); builder.ConfigureHttpClient(configureClient); builder.AddTypedClientCore <TClient, TImplementation>(validateSingleType: false); // name was explicitly provided return(builder); }
/// <summary> /// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures /// a named <see cref="HttpClient"/>. /// </summary> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="name">The logical name of the <see cref="HttpClient"/> to configure.</param> /// <param name="configureClient">A delegate that is used to configure an <see cref="HttpClient"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> /// <remarks> /// <para> /// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using /// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name. /// </para> /// <para> /// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client. /// </para> /// </remarks> public static IHttpClientBuilder AddHttpClient(this IServiceCollection services, string name, Action <IServiceProvider, HttpClient> configureClient) { ThrowHelper.ThrowIfNull(services); ThrowHelper.ThrowIfNull(name); ThrowHelper.ThrowIfNull(configureClient); AddHttpClient(services); var builder = new DefaultHttpClientBuilder(services, name); builder.ConfigureHttpClient(configureClient); return(builder); }
/// <summary> /// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures /// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>. /// </summary> /// <typeparam name="TClient"> /// The type of the typed client. The type specified will be registered in the service collection as /// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients. /// </typeparam> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="name">The logical name of the <see cref="HttpClient"/> to configure.</param> /// <param name="configureClient">A delegate that is used to configure an <see cref="HttpClient"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> /// <remarks> /// <para> /// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using /// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name. /// </para> /// <para> /// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" /> /// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing /// <typeparamref name="TClient"/> as the service type. /// </para> /// <para> /// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client. /// </para> /// </remarks> public static IHttpClientBuilder AddHttpClient <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>( this IServiceCollection services, string name, Action <IServiceProvider, HttpClient> configureClient) where TClient : class { ThrowHelper.ThrowIfNull(services); ThrowHelper.ThrowIfNull(name); ThrowHelper.ThrowIfNull(configureClient); AddHttpClient(services); var builder = new DefaultHttpClientBuilder(services, name); builder.ConfigureHttpClient(configureClient); builder.AddTypedClientCore <TClient>(validateSingleType: false); // name was explicitly provided return(builder); }
/// <summary> /// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures /// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>. The client name will /// be set to the type name of <typeparamref name="TClient"/>. /// </summary> /// <typeparam name="TClient"> /// The type of the typed client. The type specified will be registered in the service collection as /// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients. /// </typeparam> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="configureClient">A delegate that is used to configure an <see cref="HttpClient"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> /// <remarks> /// <para> /// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using /// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name. /// </para> /// <para> /// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" /> /// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing /// <typeparamref name="TClient"/> as the service type. /// </para> /// </remarks> public static IHttpClientBuilder AddHttpClient <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>( this IServiceCollection services, Action <IServiceProvider, HttpClient> configureClient) where TClient : class { ThrowHelper.ThrowIfNull(services); ThrowHelper.ThrowIfNull(configureClient); AddHttpClient(services); string name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false); var builder = new DefaultHttpClientBuilder(services, name); builder.ConfigureHttpClient(configureClient); builder.AddTypedClientCore <TClient>(validateSingleType: true); return(builder); }
/// <summary> /// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures /// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>. The client name will /// be set to the type name of <typeparamref name="TClient"/>. /// </summary> /// <typeparam name="TClient"> /// The type of the typed client. They type specified will be registered in the service collection as /// a transient service. See <see cref="ITypedHttpClientFactory" /> for more details about authoring typed clients. /// </typeparam> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="configureClient">A delegate that is used to configure an <see cref="HttpClient"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> /// <remarks> /// <para> /// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using /// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name. /// </para> /// <para> /// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" /> /// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing /// <typeparamref name="TClient"/> as the service type. /// </para> /// </remarks> public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection services, Action<HttpClient> configureClient) where TClient : class { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configureClient == null) { throw new ArgumentNullException(nameof(configureClient)); } AddHttpClient(services); var builder = new DefaultHttpClientBuilder(services, typeof(TClient).Name); builder.ConfigureHttpClient(configureClient); builder.AddTypedClient<TClient>(); return builder; }
/// <summary> /// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures /// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>. The client name will /// be set to the type name of <typeparamref name="TClient"/>. /// </summary> /// <typeparam name="TClient"> /// The type of the typed client. They type specified will be registered in the service collection as /// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients. /// </typeparam> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="configureClient">A delegate that is used to configure an <see cref="HttpClient"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> /// <remarks> /// <para> /// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using /// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name. /// </para> /// <para> /// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" /> /// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing /// <typeparamref name="TClient"/> as the service type. /// </para> /// </remarks> public static IHttpClientBuilder AddHttpClient <TClient>(this IServiceCollection services, Action <IServiceProvider, HttpClient> configureClient) where TClient : class { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configureClient == null) { throw new ArgumentNullException(nameof(configureClient)); } AddHttpClient(services); var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false); var builder = new DefaultHttpClientBuilder(services, name); builder.ConfigureHttpClient(configureClient); builder.AddTypedClient <TClient>(); return(builder); }
/// <summary> /// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures /// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>. The client name will /// be set to the type name of <typeparamref name="TClient"/>. /// </summary> /// <typeparam name="TClient"> /// The type of the typed client. They type specified will be registered in the service collection as /// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients. /// </typeparam> /// <typeparam name="TImplementation"> /// The implementation type of the typed client. They type specified will be instantiated by the /// <see cref="ITypedHttpClientFactory{TImplementation}"/> /// </typeparam> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="configureClient">A delegate that is used to configure an <see cref="HttpClient"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> /// <remarks> /// <para> /// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using /// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name. /// </para> /// <para> /// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" /> /// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing /// <typeparamref name="TClient"/> as the service type. /// </para> /// </remarks> public static IHttpClientBuilder AddHttpClient <TClient, TImplementation>(this IServiceCollection services, Action <HttpClient> configureClient) where TClient : class where TImplementation : class, TClient { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configureClient == null) { throw new ArgumentNullException(nameof(configureClient)); } AddHttpClient(services); var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false); var builder = new DefaultHttpClientBuilder(services, name); builder.ConfigureHttpClient(configureClient); builder.AddTypedClientCore <TClient, TImplementation>(validateSingleType: true); return(builder); }
/// <summary> /// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures /// a named <see cref="HttpClient"/>. /// </summary> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="name">The logical name of the <see cref="HttpClient"/> to configure.</param> /// <param name="configureClient">A delegate that is used to configure an <see cref="HttpClient"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> /// <remarks> /// <para> /// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using /// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name. /// </para> /// <para> /// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client. /// </para> /// </remarks> public static IHttpClientBuilder AddHttpClient(this IServiceCollection services, string name, Action<HttpClient> configureClient) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } if (configureClient == null) { throw new ArgumentNullException(nameof(configureClient)); } AddHttpClient(services); var builder = new DefaultHttpClientBuilder(services, name); builder.ConfigureHttpClient(configureClient); return builder; }
/// <summary> /// Adds the <see cref="IHttpClientFactory"/> and related services to the <see cref="IServiceCollection"/> and configures /// a binding between the <typeparamref name="TClient" /> type and a named <see cref="HttpClient"/>. The client name will /// be set to the type name of <typeparamref name="TClient"/>. /// </summary> /// <typeparam name="TClient"> /// The type of the typed client. The type specified will be registered in the service collection as /// a transient service. See <see cref="ITypedHttpClientFactory{TClient}" /> for more details about authoring typed clients. /// </typeparam> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="configureClient">A delegate that is used to configure an <see cref="HttpClient"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> /// <remarks> /// <para> /// <see cref="HttpClient"/> instances that apply the provided configuration can be retrieved using /// <see cref="IHttpClientFactory.CreateClient(string)"/> and providing the matching name. /// </para> /// <para> /// <typeparamref name="TClient"/> instances constructed with the appropriate <see cref="HttpClient" /> /// can be retrieved from <see cref="IServiceProvider.GetService(Type)" /> (and related methods) by providing /// <typeparamref name="TClient"/> as the service type. /// </para> /// </remarks> public static IHttpClientBuilder AddHttpClient <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>( this IServiceCollection services, Action <HttpClient> configureClient) where TClient : class { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configureClient == null) { throw new ArgumentNullException(nameof(configureClient)); } AddHttpClient(services); string name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false); var builder = new DefaultHttpClientBuilder(services, name); builder.ConfigureHttpClient(configureClient); builder.AddTypedClientCore <TClient>(validateSingleType: true); return(builder); }