title: Register multiple complex dependencies with Microsoft DI
body: <p>My ASP.NET core application uses a software client consisting of a set of classes (&quot;Helper&quot;, &quot;Client&quot;, &quot;UserSetting&quot;, &quot;ServerSetting&quot;) retrieving their correct configurations/instances from dependency injection. A single class can be configured as a singleton within the application and get used. But how can I have multiple instances of this set of classes?
  How can the instance A of &quot;Client&quot; and &quot;Helper&quot; now receive the corresponding settings for A, while the instance B receives its different corresponding values?</p>
  <p>What I have: A single client has a helper, and settings in appsettiongs.json. Both, helper and Client require the settings. All are able to communicate</p>
  <p>Targeted case: multiple instances of classes &quot;Client&quot; &quot;Helper&quot;, &quot;UserOptions&quot;, &quot;ServerOptions&quot;. As shown, I need multiple instances of this packages classes to connect to multiple servers and communicate with a selected one. Each Client must be configured for another server/user data combination.</p>
  <p>Current initialization of a single client:</p>
  <pre><code>class Startup {
    IConfiguration configuration {get;}
    public Startup(){//init configuration from appsettings.json} 
  
    public void ConfigureServices(IServiceCollection services){
      ... 
      services.Configure&lt;UserSettings&gt;(Configuration.GetSection(&quot;userData&quot;);
      service.Configure&lt;ServerSettings&gt;(Configuration.GetSection(&quot;serverSettings&quot;);
      service.AddSingleton&lt;IHelper, MyHelper&gt;();
      service.AddSingleton&lt;IClient, MyClient&gt;();
    }
  }
  
  public class MyClient:IClient{
    public void MyClient(ILogger&lt;MyClient&gt; log, IHelper helper){..}
  }
  
  public class MyHelper:IHelper{
    public void MyHelper(ILogger&lt;MyHelper&gt; log, IOptions&lt;UserSettings&gt; userSettings, IOptions&lt;ServerSettings&gt; serverSettings){..}
  }
  
  class MyWebController{
    public IClient Client;
  
    public MyWebController(IClient client){ this.Client = client;}
  
    public ActionResult DoSomeServerStuff(string data){
       Client.Communicate();
    }
  }
  </code></pre>
  <p>Idea of a preferred usage:</p>
  <pre><code>class Startup {
    IConfiguration configuration {get;}
    public Startup(){//init configuration from appsettings.json} 
  
    public void ConfigureServices(IServiceCollection services){
      { 
        //configure in some scope for A
        services.Configure&lt;UserSettings&gt;(Configuration.GetSection(&quot;ServerA:userData&quot;);
        service.Configure&lt;ServerSettings&gt;(Configuration.GetSection(&quot;ServerA:serverSettings&quot;);
        service.AddSingleton&lt;IHelper, MyHelper&gt;();
        service.AddSingleton&lt;IClient, MyClient&gt;();
      }
      {  
        //configure in some scope for B (How can I kind of scope it within those brackets?)
        services.Configure&lt;UserSettings&gt;(Configuration.GetSection(&quot;ServerB:userData&quot;);
        service.Configure&lt;ServerSettings&gt;(Configuration.GetSection(&quot;ServerB:serverSettings&quot;);
        service.AddSingleton&lt;IHelper, MyHelper&gt;();
        service.AddSingleton&lt;IClient, MyClient&gt;();
      }
    }
  }
  
  class MyWebController{
    public IClient ClientA, ClientB;
  
    public MyClass(Magic magic){
      ClientA = magic.GetClient(&quot;A&quot;);
      ClientB = magic.GetClient(&quot;B&quot;);
    }
  
    // GET: MyWebController/
    public ActionResult Index(string data){
       switch(data){
         case &quot;A&quot;: 
           ServerA.Communicate();
           break;
         case &quot;B&quot;:
           ServerB.Communicate();
           break;
       }
    }
  </code></pre>
  <p>I tried packaging the initialization into a container, to initialize it manually, but failed to create the object IOptions as requested by the constructor. The UserSettings value can not be casted to an IOptons value.</p>
  <pre><code>class MyContainer{
    public IClient ClientA, ClientB;
    public MyContainer(IConfiguration config, ILoggerFactory factory){
      userSettingsA = config.GetSection(&quot;ServerA:userData&quot;).Get&lt;UserSettings&gt;();
      serverSettingsA = config.GetSection(&quot;ServerA:serverSettings&quot;).Get&lt;ServerSettings();
      var helperA = new MyHelper(factory.CreateLogger&lt;MyHelper&gt;, userSettingsA, serverSettingsA);
      ClientA = new MyClient(factory.CreateLogger&lt;Client&gt;(), helper);
      
      // ClientB configured analog to ClientA
    }
  }
  class Startup{
    public ConfigureServices(IServiceCollection services){
      service.AddSingleton(MyContainer);
    }
  }
  </code></pre>
  <p>However, this fails, as userSettingsA as instance of UserSettings is not castable to IOptions. So I failed even with the unrecommended, explicit construction of the classes, skipping the DI.</p>
  <p>Any hints on how to proceed? Any ideas on how to scope the objects during creation with DI or how to create those objects manually by creating the IOptions type?</p>