title: Custom IServiceProviderFactory in ASP.NET Core
body: <p>I wrote a custom <code>IServiceProviderFactory</code> and installed it in <code>Program.cs</code> of a new app like this:</p>
  <pre class="lang-cs prettyprint-override"><code>    public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
          Host.CreateDefaultBuilder(args)
              .UseServiceProviderFactory(new PropertyInjectingContainerFactory())
              .ConfigureWebHostDefaults(webBuilder =&gt;
              {
                  webBuilder.UseStartup&lt;Startup&gt;();
              });
  </code></pre>
  <p>It does lead to the correct configure function in <code>Startup.cs</code> getting called:</p>
  <pre class="lang-cs prettyprint-override"><code>   public void ConfigureContainer(PropertyInjectingContainerFactory.Builder builder)
     {
          builder.AddInjectAttribute&lt;InjectDependencyAttribute&gt;();
     }
  </code></pre>
  <p>However, my created container only ever resolves two services: <code>IConfiguration</code> and <code>IHost</code>.</p>
  <p>Everything else is resolved by the default container apparantly (for instance a service like <code>ILogger&lt;T&gt;</code> on a controller). What do I do wrong?</p>
  <p>Here's the code for my custom factory - and please understand that I probably should be using an existing third-party container, but I also want to understand how this all fits together.</p>
  <pre class="lang-cs prettyprint-override"><code>public class PropertyInjectingContainerFactory : IServiceProviderFactory&lt;PropertyInjectingContainerFactory.Builder&gt;
  {
      public Builder CreateBuilder(IServiceCollection services)
      {
          return new Builder(services);
      }
  
      public IServiceProvider CreateServiceProvider(Builder containerBuilder)
      {
          return containerBuilder.CreateServiceProvider();
      }
  
      public class Builder
      {
          internal readonly IServiceCollection services;
  
          internal List&lt;Type&gt; attributeTypes = new List&lt;Type&gt;();
  
          public Builder(IServiceCollection services)
          {
              this.services = services;
          }
  
          public Builder AddInjectAttribute&lt;A&gt;()
              where A : Attribute
          {
              attributeTypes.Add(typeof(A));
  
              return this;
          }
  
          public IServiceProvider CreateServiceProvider()
              =&gt; new PropertyInjectingServiceProvider(services.BuildServiceProvider(), attributeTypes.ToArray());
      }
  
      class PropertyInjectingServiceProvider : IServiceProvider
      {
          private readonly IServiceProvider services;
          private readonly Type[] injectAttributes;
  
          public PropertyInjectingServiceProvider(IServiceProvider services, Type[] injectAttributes)
          {
              this.services = services;
              this.injectAttributes = injectAttributes;
          }
  
          // This function is only called for `IConfiguration` and `IHost` - why?
          public object GetService(Type serviceType)
          {
              var service = services.GetService(serviceType);
  
              InjectProperties(service);
  
              return service;
          }
  
          private void InjectProperties(Object target)
          {
              var type = target.GetType();
  
              var candidateProperties = type.GetProperties(System.Reflection.BindingFlags.Public);
  
              var props = from p in candidateProperties
                          where injectAttributes.Any(a =&gt; p.GetCustomAttributes(a, true).Any())
                          select p;
  
              foreach (var prop in props)
              {
                  prop.SetValue(target, services.GetService(prop.PropertyType));
              }
          }
      }
  }
  </code></pre>