skip to Main Content

I’m working on a solution that interacts with Redis, using the servicestack.net library.
I have a class that inherits from ServiceStack.AppHostBase and asks me for an override of the Configure method. This method has as a parameter a Funq.Container that I see is an implementation of IServiceProvider, IResolver and IContainer, and none of these interfaces have the AddHttpClient method that is provided by the IServiceCollection. Method I need to be able to inject the IHttpClientFactory. Any idea how to solve my problem?

2

Answers


  1. This is the interface definition of IServiceCollection from IServiceCollection.cs:

    public interface IServiceCollection : IList<ServiceDescriptor>
    {
    }
    

    AddHttpClient is just an extension method from Microsoft.Extensions.DependencyInjection that wraps adding a number of additional dependencies to ASP.NET Core IOC.

    So you should continue to register it on ASP.NET Core IOC, i.e:

    public class Startup : ModularStartup
    {
        public new void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpClient();
        }
    
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseServiceStack(new AppHost
            {
                AppSettings = new NetCoreAppSettings(Configuration)
            });
        }
    }
    

    As any dependencies registered .NET Core Startup are also available to ServiceStack.

    Login or Signup to reply.
  2. To do it in ASP.NET (not .NET Core), the quick way would be to:

    1. install Microsoft.Extensions.DependencyInjection package and call .AppHttpClient() extension
    2. Build the Service Provider you would normally see in .NET Core
    3. Get the instance of IHttpClientFactory from the Service Provider
    4. Register the instance of IHttpClientFactory with Funq.Container again
    using Microsoft.Extensions.DependencyInjection;
    
    public class AppHost : AppHostBase
    {
        public override void Configure(Container container)
        {
            ...
    
            RegisterHttpClientFactory(container);
        }
    
        private container RegisterHttpClientFactory(Container container)
        {
            var services = new ServiceCollection()
                .AddHttpClient();
            
            // You can kind of inspect services returned.
            // You can see this extension registers lot of other things too beside
            // IHttpClientFactory.
            // Also you can see the lifetime of IHttpClientFactory is Singleton.
    
            var serviceProvider = services.BuildServiceProvider();
    
            container.AddSingleton(serviceProvider.GetService<IHttpClientFactory>());
    
            return container;
        }
    }
    

    If you happen to use Unity Adaptor

    Unity has a package to give you an extension as well to build the Service Provider directly into the Unity Container:

    using Microsoft.Extensions.DependencyInjection;
    using Unity;
    using Unity.Microsoft.DependencyInjection;
    
    public static class UnityConfig
    {
        public static void RegisterTypes(IUnityContainer container)
        {
            ...
            container.RegisterServices();
            container.RegisterHttpClientFactory();
        }
    
        private static IUnityContainer RegisterHttpClientFactory(
            this IUnityContainer unityContainer)
        {
            new ServiceCollection()
                .AddHttpClient()
                .BuildServiceProvider(unityContainer);
    
            return unityContainer;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search