Exemple #1
0
        /// <summary>
        /// Adds a Refit client to the DI container
        /// </summary>
        /// <typeparam name="T">Type of the Refit interface</typeparam>
        /// <param name="services">container</param>
        /// <param name="settingsAction">Optional. Action to configure refit settings.  This method is called once and only once, avoid using any scoped dependencies that maybe be disposed automatically.</param>
        /// <returns></returns>
        public static IHttpClientBuilder AddRefitClient <T>(this IServiceCollection services, Func <IServiceProvider, RefitSettings?>?settingsAction) where T : class
        {
            services.AddSingleton(provider => new SettingsFor <T>(settingsAction?.Invoke(provider)));
            services.AddSingleton(provider => RequestBuilder.ForType <T>(provider.GetRequiredService <SettingsFor <T> >().Settings));

            return(services
                   .AddHttpClient(UniqueName.ForType <T>())
                   .ConfigureHttpMessageHandlerBuilder(builder =>
            {
                // check to see if user provided custom auth token
                if (CreateInnerHandlerIfProvided(builder.Services.GetRequiredService <SettingsFor <T> >().Settings) is {} innerHandler)
                {
                    builder.PrimaryHandler = innerHandler;
                }
            })
                   .AddTypedClient((client, serviceProvider) => RestService.For <T>(client, serviceProvider.GetService <IRequestBuilder <T> >() !)));
        }
Exemple #2
0
        /// <summary>
        /// Adds a Refit client to the DI container
        /// </summary>
        /// <param name="services">container</param>
        /// <param name="refitInterfaceType">Type of the Refit interface</param>
        /// <param name="settingsAction">Optional. Action to configure refit settings.  This method is called once and only once, avoid using any scoped dependencies that maybe be disposed automatically.</param>
        /// <returns></returns>
        public static IHttpClientBuilder AddRefitClient(this IServiceCollection services, Type refitInterfaceType, Func <IServiceProvider, RefitSettings?>?settingsAction)
        {
            var settingsType       = typeof(SettingsFor <>).MakeGenericType(refitInterfaceType);
            var requestBuilderType = typeof(IRequestBuilder <>).MakeGenericType(refitInterfaceType);

            services.AddSingleton(settingsType, provider => Activator.CreateInstance(typeof(SettingsFor <>).MakeGenericType(refitInterfaceType) !, settingsAction?.Invoke(provider)) !);
            services.AddSingleton(requestBuilderType, provider => RequestBuilderGenericForTypeMethod.MakeGenericMethod(refitInterfaceType).Invoke(null, new object?[] { ((ISettingsFor)provider.GetRequiredService(settingsType)).Settings }) !);

            return(services
                   .AddHttpClient(UniqueName.ForType(refitInterfaceType))
                   .ConfigureHttpMessageHandlerBuilder(builder =>
            {
                // check to see if user provided custom auth token
                if (CreateInnerHandlerIfProvided(((ISettingsFor)builder.Services.GetRequiredService(settingsType)).Settings) is { } innerHandler)
                {
                    builder.PrimaryHandler = innerHandler;
                }
            })
                   .AddTypedClient(refitInterfaceType, (client, serviceProvider) => RestService.For(refitInterfaceType, client, (IRequestBuilder)serviceProvider.GetRequiredService(requestBuilderType))));
        }
 /// <summary>
 /// Adds a Refit client to the DI container
 /// </summary>
 /// <param name="services">container</param>
 /// <param name="refitInterfaceType">Type of the Refit interface</typeparam>
 /// <param name="settings">Optional. Settings to configure the instance with</param>
 /// <returns></returns>
 public static IHttpClientBuilder AddRefitClient(this IServiceCollection services, Type refitInterfaceType, RefitSettings settings = null)
 {
     return(services.AddHttpClient(UniqueName.ForType(refitInterfaceType))
            .AddTypedClient((client, serviceProvider) => RestService.For(refitInterfaceType, client, settings)));
 }